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.
$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 haven't displayed the HTML for it. I'm a beginner so I'm not sure where I've gone wrong, I'm not sure if I can have two PDO execute statements under the same name variable.
Any help or tips will be appreciated
UPDATE: One checkbox will now display all relevant info (without the checklist[]) but it doesn't work for two of them(I believe I need the checklist[] to make the two work) so my new question is, how do I make it so that :
$search = '%' . $searchTerm . '%';
displays more then the one searchTerm