How to submit a form to the same page?

Posted on 16th Feb 2014 by admin

I have a table containing information about books in my library and this table has the following columns: id, title, category. What I want to do is to make a form that will allow me to sort these books according to one of these fields, its just like in you tube when you want to sort videos by date added, relevance, view count etc..

Both the form and the php code are in "view_books.php"

Here is my form:

Code: <form method="POST" action="view_books.php">

Sort by:

<select size="1" name="sort">

<option> id </option>

<option> category </option>

<option> Date Added </option>

</select>

<input type="submit" value="Go!">

</form>

Here is my php code to sort the books in a table:

Code: $query = mysql_query ("SELECT * FROM books ORDER BY id asc ") or die (mysql_error());

?>

<table width="76%" border="0" align="center" >

<?

while($rows = mysql_fetch_array( $query ))
{

?>

<tr>

<td> <font color="#696969" size=2> <?php echo $rows['id']; ?> </font></td>
<td> <font color="#696969" size=2> <?php echo $rows['title']; ?> </font></td>
<td> <font color="#696969" size=2> <?php echo $rows['category']; ?> </font></td>

</tr>

<?

}

?>

</table>

Please apply the changes in my code so that it can perform the required function.

Thanks, any help would be appreciated.

Other forums