I have a bit of a problem. I have a navbar
| home | view | view(active) | view(deleted) |
When I click on view, I am taken to view.php which just retrieves all database records with:
SELECT * FROM inventory
In my inventory table, I have a column called status that has a value of either y or n. (y for active, n for not active (or deleted)).
When I click on the view(active) and view(deleted) links I want to execute different queries, such as these:
SELECT * FROM inventory WHERE status LIKE 'y' (for retrieval of active records)
SELECT * FROM inventory WHERE status LIKE 'n' (for retrieval of deleted records)
It works fine if I attach ?view=y to the end of my view.php link. The form action of my form references itself (view.php) except with the view type attached to it: ?view=y or ?view=n and then having conditionals determine which mysql query to execute. However, I would like to handle this situation with POST and it’s not working. I don’t want any values in my URL. Works just fine with $_GET though.
Thanks.
CODE (view.php conditionals for queries)
if ($_POST['view'] == "y") {
$result = mysql_query("SELECT * FROM inventory WHERE status LIKE 'y'"); }
else if ($_POST['view'] == "n") {
$result = mysql_query("SELECT * FROM inventory WHERE status LIKE 'n'"); }
else if ($_POST['submit'] == "search") {
$result = mysql_query("SELECT * FROM inventory WHERE descrip LIKE '%$searchString%'"); }
else {
$result = mysql_query("SELECT * FROM inventory"); }
CODE (my navigation bar)
<div id="navigation">
<a href="add.php"><input type="button" value="ADD" /></a>
<a href="view.php"><input type="button" value="VIEW" /></a>
<a href="view.php?view=y"><input type="button" value="view active" /></a>
<a href="view.php?view=n"><input type="button" value="view deleted" /></a>
<div><br />
<form action="view.php" method="POST">
<input type="text" name="searchDescription" id="search" placeholder="search for text in description" value="<?php echo $_POST['searchDescription']; ?>"/>
<input type="submit" value="SEARCH" name="submit" id="searchDescriptionButton" />
</form>
</div>
</div>
You can use hidden input types to pass POST parameters to your php script. There are a lot of more elegant solutions, but here is one that requires minimum code change.
Add
<input type="hidden" name="view" id="viewvalue">in your form, and have your buttons call a javascript function that sets the value toyornand submits the form.Note that this function assumes that you have given the id
viewformto your form.Then change the links in your buttons’
a hreftojavascript:setView('y')andjavascript:setView('n), and you should be set.EDIT : Since you cannot use javascript, here is what it should look like