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>
<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 frommysqltomysqliorPDO