louis
louis

Reputation: 11

How to filter search results in PHP using radio buttons/checkbox form?

I've already created a search bar wherein users can enter keywords to search for but I also placed radio buttons below it so that when they enter their keyword, the search results would immediately filter it.

Here is what it looks like in the page

Image of the Search Bar with buttons

Here is my index.php:

<section id = "search">
        <div class="container">
            <form action="search.php">
            <input type="text" name= "Search" id="Search" placeholder="Enter a keyword">
            </form>
        </div>
    </section>


<!--radio buttons and checkbox-->
<section id="sort">
        <div class="container">
            <form action="search.php">
                <input type="radio" id="all" name="Search" value="all" checked>
                <label for="all">All</label><br><br>

                <input type="radio" id="subject" name="Search" value="subject">
                <label for="subject"> Subject/s : </label><br>
                <input type="checkbox" id="english" name="Search" value="English">
                <label for="subject">English</label>
                <input type="checkbox" id="science" name="Search" value="Science">
                <label for="subject">Science</label>
                <input type="checkbox" id="math" name="Search" value="Math">
                <label for="subject">Math</label>
            </form>
        </div>
    </section>

Here is my search.php:

<?php
include "databaseconnect.php";

$keywordfromform = $_GET["Search"];

$sql = ("SELECT titleID, authorsID, yearID, subjectID 
        FROM researchpapertable 
        WHERE titleID LIKE '%" . $keywordfromform . "%'
        OR authorsID LIKE '%" . $keywordfromform . "%'
        OR yearID LIKE '%" . $keywordfromform . "%'
        OR subjectID LIKE '%" . $keywordfromform . "%'
        ");
$result = $mysqli->query($sql);

if ($result-> num_rows>0) {
  // output data of each row
while($row = mysqli_fetch_assoc($result)) {
    echo '<tr><td><a href="upload.php">'. $row["titleID"]."</a></td><td>". $row["authorsID"]."</td><td>". $row["yearID"]."</td><td>". $row["subjectID"]."</td></tr>";
  }

} else {
  echo "<tr><td> 0 results </td><td> 0 results </td><td> 0 results </td><td> 0 results </td><tr>";
}

$mysqli->close();

?>

What do I need to add in order for it to filter its search as well? It works when I enter a keyword but it does not recognize what I have placed in the buttons.

I am not sure what I should search for in order to search for similar tutorials/code. If there are similar questions/code to this, it would help a lot.

This is my first site so thank you in advance!

Upvotes: 0

Views: 1267

Answers (2)

Esteban MANSART
Esteban MANSART

Reputation: 561

As Ro said, first, you need to give search input and checkboxes a different name. (But same name for all checkboxes because they affect a same filter).

Then, I think we need more details about the construction of your database.

What I suppose to you, without understanding your structure, is to search with OR and LIKE keywords as you already did and add the IN keyword in your query for the subject.

I could be something like this :

// Your HTML file
<input type="radio" id="subject" name="search" value="subject">
<label for="subject"> Subject/s : </label><br>
<input type="checkbox" id="filterEnglish" name="subjectFilter[]" value="English">
<label for="filterEnglish">English</label>
<input type="checkbox" id="filterScience" name="subjectFilter[]" value="Science">
<label for="filterScience">Science</label>
<input type="checkbox" id="filterMath" name="subjectFilter[]" value="Math">
<label for="filterMath">Math</label>

The name of the checkboxes have to be followed by [] to add values to PHP $_GET variable

// Your PHP file
$yourInputTextSearch = $_GET["search"]
$subjectFilters = $_GET["subjectFilter"]

$sql = 
"SELECT titleID, authorsID, yearID, subjectID 
    FROM researchpapertable 
    WHERE 
    (
        titleID LIKE '%" . $yourInputTextSearch. "%'
        OR authorsID LIKE '%" . $yourInputTextSearch. "%'
        OR yearID LIKE '%" . $yourInputTextSearch. "%'
    )
    AND subjectID IN ('" . implode("','", $subjectFilters). "')
    ");

Where $subjectFilter array of your checked subjects

Keep in mind that the values received in $subjectFilter var are the values of the value attribute you made in your HTML form, so, you have to make the exact values you have in your database.

Upvotes: 1

Ro Achterberg
Ro Achterberg

Reputation: 2694

All your form inputs share the same name. PHP will record the first occurrence (your type="text").

You'll have to format your SQL slightly differently according to which filter options were chosen.

You will also greatly improve the UX if you got rid of both the radio buttons, and just left the checkboxes as filter options. You should group them an give them a descriptive form group heading.

Upvotes: 0

Related Questions