Reputation: 359
Basically I'm using an HTML form and PHP to connect to a MySQL database, and I want the user to be able to choose how they want to sort the results from a drop-down menu before the results are actually returned.
Here's my form:
<p>View the complete list of books as sorted by:
<form action="usersort.php" method="post">
<select name="sortby"><option value="book_name">Book Title</option>
<option value="author">Author</option></select>
<input type="submit" name="submit" value="Go"></form></p>
And here's my PHP:
<?php
echo '<table align="center" cellspacing="0" cellpadding="5" width="75%"> <tr>
<td align="left"><b><a href="usersort.php?sort=book_name">Book Name</a></b></td>
<td align="left"><b><a href="usersort.php?sort=author">Author</a></b></td>';
require ('mysqli_connect.php');
$sort = (isset($_GET['sortby'])) ? $_GET['sortby'] : 'books';
switch($sort) {
case 'book_name':
$order_by = 'book_name ASC';
break;
case 'author':
$order_by = 'author ASC';
break;
default:
$order_by = 'book_name ASC';
$sort = 'books';
break;
}
$q = "SELECT (book_name) AS name, (author) AS author, (publisher) AS publisher FROM books ORDER BY $order_by";
$r = @mysqli_query ($dbc, $q);
if ($r) {
echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
<tr><td align="left"><b>Book Name</b></td><td align="left"><b><Author</b></td></tr>';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC))
{echo '<tr><td align="left">' . $row['name'] . '</td><td align = "left">' . $row['author'] . '</td><td align = "left">' . $row['publisher'] . '</td></tr>';
}
echo '</table>';
mysqli_free_result($r);
} else {
echo 'The current books could not be retrieved.';
echo '<p>' . mysqli_error($dbc) . '<br><br>Query: ' . $q . '</p>';
}
?>
I assumed that since I named the select value in the HTML form as "sortby" that my PHP would retrieve the value and insert it for "sortby" but whenever I try to access the return through the form, it always just uses the default case in the switch condition? Can someone tell me what I'm doing wrong?
Upvotes: 2
Views: 1726
Reputation: 1058
$sort = (isset($_POST['sortby'])) ? $_POST['sortby'] : 'book_name'; //<< HERE
switch($sort) {
case 'book_name':
$order_by = 'book_name ASC';
break;
case 'author':
$order_by = 'author ASC';
break;
default:
$order_by = 'book_name ASC';
$sort = 'book_name'; //<< AND HERE
break;
}
Upvotes: 0
Reputation: 7821
It's because your form action is set to post
rather than get
. Your PHP isset
is looking for a global $_GET
when it should be looking for a $_POST
.
The line should be changed to:
$sort = (isset($_POST['sortby'])) ? $_POST['sortby'] : 'books';
You should also clean up the indentation on your switch
statement:
switch($sort) {
case 'book_name':
$order_by = 'book_name ASC';
break;
case 'author':
$order_by = 'author ASC';
break;
default:
$order_by = 'book_name ASC';
$sort = 'books';
break;
}
Upvotes: 0
Reputation: 20229
You post the form and get the value by using $_POST Instead of $_GET
Change
$sort = (isset($_GET['sortby'])) ? $_GET['sortby'] : 'books';
to
$sort = (isset($_POST['sortby'])) ? $_POST['sortby'] : 'books';
Upvotes: 1