0

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!

1
  • check th content of $getm, you will find all elements there Commented Oct 31, 2020 at 17:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Please don't ever insert user input directly into a query. Use a prepared statement instead.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.