Hi
I have written some php code that displays a dropdown list and when the
user selects a value from the list and clicks the “Go” button, the form
displays a filtered database view based on the selection.
Once the user clicks the “Go” button, the filtered records are displayed correctly,
But how do I set the appropriate selection in the dropdown box to be showing.
Right now, it defaults back to the first option value as I am not sure how to do this.
Any help, much appreciated.
Javascript code:
function FilterSchools(selectObject)
{
var value = selectObject.value;
document.getElementById("ftype").value = value;
}
<?php
function xxx_schools_list()
{
?>
<link type="text/css" href="<?php echo WP_PLUGIN_URL; ?>/xxx-schools/style-admin.css" rel="stylesheet" />
<script language="JavaScript" src="<?php echo WP_PLUGIN_URL; ?>/xxx-schools/tenders.js"></script>
<div class="wrap">
<h2 style="font-size:28px;color:#0c6bb5;">School List</h2>
<div class="tablenav top">
<div class="alignleft actions">
<form name="Form1" action="" method="POST">
<select name="filter" onchange="FilterSchools(this)">
<option value=''>All</option>
<option value='Open'>Open</option>
<option value='Closed'>Closed</option>
<option value='Awarded'>Awarded</option>
<option value='Archived'>Archived</option>
<option value='Cancelled'>Cancelled Tenders</option>
</select>
<input type="text" id="ftype" name="ftype">
<input type="submit" class="fbutton-small" value="GO">
</form>
<br/><br/><br/>
</div>
<br class="clear">
</div>
<?php
global $wpdb;
$sFilter = $_POST["ftype"];
$_SESSION["filterby"] = $sFilter;
$table_name = $wpdb->prefix . "school";
if ($sFilter == '')
{
$rows = $wpdb->get_results("SELECT * from $table_name ORDER BY id");
}
else
{
$rows = $wpdb->get_results("SELECT * from $table_name WHERE status = '$sFilter' ORDER BY id");
}
?>
<table class="wp-list-table widefat fixed striped posts" width=100%>
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Issued</th>
<th>Closed</th>
<th>Status</th>
<th>Description</th>
<th> </th>
</tr>
<?php foreach ($rows as $row)
{
?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->type; ?></td>
<td><?php echo $row->issued; ?></td>
<td><?php echo $row->closed; ?></td>
<td><?php echo $row->status; ?></td>
<td><?php echo substr($row->desc, 0, 60); ?></td>
<td><a href="<?php echo admin_url('admin.php?page=xxx_schools_update&id=' . $row->id); ?>">Update</a></td>
</tr>
<?php } ?>
</table>
</div>
<?php
}
?>