Greetings people, I'm currently having an issue with a simple HTML form that you can use to alter the sorting of a list of data from a MySQL table. It's just 2 drop down boxes (what to sort on and asc or desc) with an update button. My issue is that the button seem to merely refresh the page, the selected options in the 2 drop down boxes are ignored.
I have a SQL table in which quotes from various movies/tv-series/games are stored and randomly displayed on a page of my website. For those who would like to see the entire list I have made a simple page that displays all of them and which you can sort on the quote, the quote author and the source of it.
The way I have setup this little system is like this;
- index file (quote_index.php) = Main file with includes to the other files. This to keep overview in the code. It would be a pretty long document otherwise.
- page link calculation (quote_page1.php) = Calculation code for page links.
- page link generation (quote_page2.php) = Generating page links when necessary
- selection menu (quote_menu.php) = The selection menu.
- showing results (quote_show.php) = Retrieving the data from the DB and show it.
Here's my code;
File - quote_index.php;
if (isset($_POST['Submit'])) {
$sortby_real = $_POST['sortby'];
$ascdesc_real = $_POST['ascdesc'];
//Selection menu;
require ('quotes_menu.php');
//Calculation for pages;
require ('quotes_pages1.php');
//Retrieving the data from the database and show it;
require ('quotes_show.php');
//Generating the page links;
require ('quotes_pages2.php');
} else {
$sortby_real = $_POST['sortby'];
$ascdesc_real = $_POST['ascdesc'];
//Selection menu;
require ('quotes_menu.php');
//Calculation for pages;
require ('quotes_pages1.php');
//Retrieving the data from the database and show it;
require ('quotes_show.php');
//Generating the page links;
require ('quotes_pages2.php');
}
File - quotes_menu.php
echo "<form method=\"post\" action=\"index.php?view=randomquotes\">
<input type=\"hidden\" name=\"action\" value=\"update\" />
<table class=\"quote-table\">
<tr>
<td width=\"25%\"><b>Sort by:</b></td>
<td width=\"25%\"><select name=\"sortby\" class=\"droplist\">
<option value=\"qut_quote\"";
if($sortby_real==qut_quote){echo "selected=\"selected\"";}
echo ">Quote</option>
<option value=\"qut_author\"";
if($sortby_real==qut_author){echo "selected=\"selected\"";}
echo ">Quote author</option>
<option value=\"qut_source\"";
if($sortby_real==qut_source){echo "selected=\"selected\"";}
echo ">Quote source</option>
</select></td>
<td width=\"25%\"><select name=\"ascdesc\" class=\"droplist\">
<option value=\"asc\"";
if($ascdesc_real==asc){echo "selected=\"selected\"";}
echo ">Ascending (A-Z)</option>
<option value=\"desc\"";
if($ascdesc_real==desc){echo "selected=\"selected\"";}
echo ">Descending (Z-A)</option>
</select></td>
<td width=\"25%\"><input type=\"submit\" name=\"submit\" class=\"tag-button\" value=\"Perform sort action\" /></td>
</tr>
</table>
</form>
File - quotes_show.php
// get the info from the db
$sql = "SELECT * FROM quotes ORDER BY $sortby_real $ascdesc_real LIMIT $offset, $rowsperpage";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
//Header of the tabel;
echo "<table class=\"arc-table-header\">
<tr>
<td width=\"60%\" class=\"yellow-bold-10\">Quote;</td>
<td width=\"40%\" class=\"yellow-bold-10\">Author and source;</td>
</tr>
</table>
<table class=\"arc-table\">";
// while there are rows to be fetched...
while ($pageQry = mysql_fetch_assoc($result)) {
?>
<tr <?php if($counter %2 != 0) {echo "class=\"arc-table-row2\"";} else {echo "class=\"arc-table-row1\"";} ?>>
<td width="60%" class="table-cell1"><?php echo $counter++; ?>.
<?php echo $pageQry['qut_quote'] ?></td>
<td width="40%" class="table-cell1"><?php echo "" . $pageQry['qut_author'] . " (" . $pageQry['qut_source'] . ")"; ?></td>
</tr>
<?php }; //End while loop
//End of table;
echo "</table>";
I already tested everything without the page link code to make sure it wasn't that and it still didn't work. That's why I didn't include the code of that because I don't think it's got anything to do with my issue.
I've normally solved issues like this by experimenting (leaving the page link code out, putting all relevant code into one PHP file, none of that worked), searching on the internet for solutions until I worked it out. I did so this time, I read several tutorials, messed with form code I used on previous projects that did work and so on but nothing worked so that's why I created this topic.