how would i get it so i can display a dropdown, and the user can select a value in the dropdown and clicks a button, and after that it displays only values in the database that match what the user selected? Something like this..
It's not really wrong. Just missing the important step:
Code:
$selectedvalue = $_GET["category"];
or
$selectedvalue = $_POST["category"];
depending on whether your <form> is coded as method="get" or method="post"
Though I don't understand the point of the $ characters in front of all the category values, unless you really do have $ characters in your database in that category field.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
The <select> *MUST* be between the <form> and the </form> !!!
Also, you need to provide for the case when the page first appears and no category has yet been chosen.
Code:
<form ...>
<select ...>
</select>
<input type="submit" ...>
</form>
<?php
$selectedvalue = $_GET["category"];
if ( isset($selectedvalue) )
{
// ** NO AMPERSAND IN THE SQL!!! **
$sql = "SELECT * from `offers` WHERE `category` = '$selectedvalue' ORDER BY id DESC";
$get_name = mysql_query($sql) ...
... dump out results ...
}
?>
Or, if you want to show *all* categories when no <option> is yet chosen, maybe
Code:
<form ...>
<select ...>
</select>
<input type="submit" ...>
</form>
<?php
$selectedvalue = $_GET["category"];
if ( isset($selectedvalue) )
{
$sql = "SELECT * from offers WHERE category = '$selectedvalue' ORDER BY id DESC";
} else {
$sql = "SELECT * from offers ORDER BY id DESC";
}
$get_name = mysql_query($sql) ...
... dump out results ...
?>
Up to you.
But in any case, no <form> fields are ever posted back to server if they don't appear in the HTML between <form> and </form>. Or if they don't have a name=
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Consider this scenario which is what I am assuming you have.
1 - You have a php page containing a <form> with a <select> inside the form.
2 - the user makes a selection from the <select> and clicks a submit button
3 - the form then sends the user's selection to the php script specified in the form's action attribute. This php script can be the same php script that contains the form or it could be a separate php script.
4 - in either case, the script processing the form data extracts the data from the database for the user's selection and then outputs the records in the browser.
So the first thing you need to decide is whether the form data processing script in 3 is to be the same script containing the original <form> or a separate script all together.
Last edited by webdev1958; 08-29-2011 at 03:09 AM..
1 - You have a php page containing a <form> with a <select> inside the form.
2 - the user makes a selection from the <select> and clicks a submit button
3 - the form then sends the user's selection to the php script specified inthe form's action attribute. This php script can be the same php script that contains the form or it could be a separate php script.
4 - in either case, the script processing the form data extracts the data from the database for the user's selection and then outputs the records in the browser.
that is exactly right. i have a search script called categorysearch.php, but when it tries to search it searches for the term "Category" instead of the users selected value.
Last edited by markman641; 08-29-2011 at 03:17 AM..
You'll have to post the actual query being run to debug it, but in theory if the user selects the Absolute Rewards optin then your query should evaluate to
Code:
SELECT * from `offers` WHERE `category` = 'Absolute Rewards' & ORDER BY id DESC
To help debug your code, do the following
PHP Code:
$query = "SELECT * from `offers` WHERE `category` = '$selectedvalue' ORDER BY id DESC";
echo $query; //this will display the actual query about to be run
die(); //stop further execution
and post the output from echo statement.
btw - I removed the ampersand that was in your original code. I don't know why you had it there.
Okay, i give up this is more complicated then i thought.
It doesn't have to be.
If you put this code in a file called searchCategory.php and insert your code to connect to the db where I have commented to do so, it should work when you substitute your own table field names for col1, col2 etc
See, from your post #4 I just *assumed* that the page with the <select> and the page that *used* the selection were/are one and the same.
YOU are the one who put the mysql_query line of code into the same page of code as the <select>. If you want them to be different pages, then need that mysql_query to be on the page that is specified in the action= of the <form>.
Read again what webdev said in his post #9. You *CAN* do it either way. Honest. It's 100% your choice. Webdev's code in post #13 is clean and clear and a way to do it all in one page. If you really want two pages, though, just ask. It's a truly minor mod to that code.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.