0

Im trying to have a simple select box that filters the database. I have successfully filled the database with all vallues on pageload, however I cant seem to get over the final hurdle of applying the filter. Also, once I have clicked the filter button, is it possible for the select text to stay at the text rather than returning to the default text?

[NOTE: I have pretty URL's enabled hence the no .php at the end]

The code is as followed:

            <?php 

            $query = "SELECT * FROM Events";
            echo $Type;

            if (isset($_POST['filter'])) {

                $Type = $_POST['value'];

                $query .= " WHERE Type = '{$Type}'";

            }               

            $result = mysql_query($query);

            ?>                      

            <form action='/events' method="post" name="form_filter" >
                <select class="eventList">
                    <option value="EVENT1">EVENT1</option>
                    <option value="EVENT2">EVENT2</option>
                    <option value="EVENT3">EVENT3</option>
                    <option value="EVENT4">EVENT4</option>
                    <option value="EVENT5">EVENT5</option>
                    <option value="EVENT6">EVENT6</option>
                    <input type="submit" name="filter" value="Filter">                      
                </select>

            </form> 

            <table class="table table-striped table-hover table-curved">
                <thead>
                    <tr>
                        <th><b>Date</b></th>
                        <th><b>Event Name</b></th>
                        <th><b>Type</b></th>
                        <th><b>Region</b></th>
                    </tr>
                </thead>
                <tbody>                 

                <?php while ($row = mysql_fetch_array($result)) { ?>
                <tr>
                    <td><?php echo $Date = $row['Date']; ?></td>
                    <td><?php echo $Name = $row['Name']; ?></td>
                    <td><?php echo $Type = $row['Type']; ?></td>
                    <td><?php echo $Region = $row['Region']; ?></td>
                </tr>
                <?php } ?>
                </tbody>
            </table>
2
  • 2
    (1) form inputs need a name attribute - <select name="eventList" class="eventList"> which you then get in php - $Type = $_POST['eventList'];. (2) you should sanitize any data that you add to your query - $Type = mysql_real_escape_string($_POST['eventList']). (3) why is your submit button <input type="submit" name="filter" value="Filter">` nested inside your <select>? (4) take the time to update from mysql to mysqli or PDO Commented May 6, 2015 at 14:54
  • @Sean Thanks for the informative message - even with all the changes it isnt working. I click the filter button and it just returns to the same page without any changes to the table Commented May 6, 2015 at 15:07

2 Answers 2

2

Your <select> does not have a name:

<select class="eventList" name="event">

Use that name as index in the $_POST array:

$Type = $_POST['event'];
Sign up to request clarification or add additional context in comments.

Comments

0

Update your code as follow

<?php 

        $query = "SELECT * FROM Events";
        echo $Type;

        if (isset($_POST)) {

            $Type = $_POST['event'];

            $query .= " WHERE Type = '{$Type}'";

        }               

        $result = mysql_query($query);

        ?>                      

        <form action='/events' method="post" name="form_filter" >
            <select class="eventList" name="event">
                <option value="EVENT1" <?php echo ($Type=="EVENT1")?'selected="selected"':'';?>>EVENT1</option>
                <option value="EVENT2" <?php echo ($Type=="EVENT2")?'selected="selected"':'';?>>EVENT2</option>
                <option value="EVENT3" <?php echo ($Type=="EVENT3")?'selected="selected"':'';?>>EVENT3</option>
                <option value="EVENT4" <?php echo ($Type=="EVENT4")?'selected="selected"':'';?>>EVENT4</option>
                <option value="EVENT5" <?php echo ($Type=="EVENT5")?'selected="selected"':'';?>>EVENT5</option>
                <option value="EVENT6" <?php echo ($Type=="EVENT6")?'selected="selected"':'';?>>EVENT6</option>
                <input type="submit" name="filter" value="Filter">                      
            </select>

        </form> 

        <table class="table table-striped table-hover table-curved">
            <thead>
                <tr>
                    <th><b>Date</b></th>
                    <th><b>Event Name</b></th>
                    <th><b>Type</b></th>
                    <th><b>Region</b></th>
                </tr>
            </thead>
            <tbody>                 

            <?php while ($row = mysql_fetch_array($result)) { ?>
            <tr>
                <td><?php echo $Date = $row['Date']; ?></td>
                <td><?php echo $Name = $row['Name']; ?></td>
                <td><?php echo $Type = $row['Type']; ?></td>
                <td><?php echo $Region = $row['Region']; ?></td>
            </tr>
            <?php } ?>
            </tbody>
        </table>

i have update this line `if (isset($_POST)) {

and added the select name and code list code

 $Type =$_POST['event'];
<select class="eventList" name="event">
<option value="EVENT1" <?php echo ($Type=="EVENT1")?'selected="selected"':'';?>>EVENT1</option>

4 Comments

this does not fix the OPs issue, which is the <select> does not have a name attribute, so all your echo ($Type=="...") will never work in addition to their db filtering not working
i have edited the code you are right, i have realized after posting
This works, however how can I edit this so that it shows all results when the page is first loaded?
it should show results when page loaded first time, but after you post the page it will show filter results, if its not the case let me know i will send you the fix

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.