I'm setting up a directory page that displays different workouts depending on what is searched through a text field(which works) and/or checkboxes(Doesn't work)
I don't know if it's possible without a submit button but I would like it to search for items as well as letting it search for multiple checkboxes
I've tried looking on the internet for help but I can't find anything that's related to my project.
What I have tried:
php:
$sqlBrowse = "SELECT * FROM workoutLibrary
WHERE name LIKE :findInName
OR bodyPart LIKE :findInBodyPart
OR muscleGroup LIKE :findInMuscleGroup
OR equipment LIKE :findInEquipment
ORDER BY name";
$searchTerm = '';
$search = '%';
if (isset($_POST['search'])) {
$searchTerm = $_POST['search'];
$search = '%' . $searchTerm . '%';
$stmt = $conn->prepare($sqlBrowse);
$stmt->bindParam(":findInName", $search);
$stmt->bindParam(":findInBodyPart", $search);
$stmt->bindParam(":findInMuscleGroup", $search);
$stmt->bindParam(":findInEquipment", $search);
$stmt->execute();
$exercises = $stmt->fetchAll();
}
else if (isset($_POST['checklist'])) {
$searchTerm = $_POST['checklist'];
$search = '%' . $searchTerm . '%';
$stmt = $conn->prepare($sqlBrowse);
$stmt->bindParam(":findInEquipment", $search);
$stmt->execute();
$exercises = $stmt->fetchAll();
}
HTML:
<form action="directory.php" method="post">
<input type="checkbox" name="checklist[]" class="check" value="Dumbell"> Dumbbell<br>
<input type="checkbox" name="checklist[]" class="check" value="kettlebell"> Kettlebell<br>
</form>
<div class="col-sm-10">
<h1 class="theTitle"> Workout Directory </h1>
<div class="d-flex flex-wrap">
<?php
foreach ($exercises as $exercise) {
?>
<div class="card" id="workoutCard">
<div class="card-header"> <?= $exercise->name ?> </div>
<div class="card-body">
<div class="thumbnail"> </div>
<div class="info">
<a id="subTitle">Body Parts:</a> <?= $exercise->bodyPart ?><BR>
<a id="subTitle">Muscle Group:</a> <?= $exercise->muscleGroup ?><BR>
<a id="subTitle">Equipment Used:</a> <?= $exercise->equipment ?>
</div>
</div>
</div>
<?php
}
?>
My search bar works perfectly so i havent displayed the html for it.
I'm a beginner so i'm not sure where i've gone wrong, like having two PDO execute statements under the same name.
Any help or tips will be appreciated