Click to See Complete Forum and Search --> : simple search using <select> box
moondance
08-27-2003, 10:19 AM
I'd like to add a simple search to the php area of my site.
I have got this working, but i would like to add a select box so the user can define whether they want to search by keyword or by index (of a customer complaint).
I have:
<select name = "SearchType">
<option>Search by....</option>
<option name = "ID">ID</option>
<option name = "Keyword">Keyword</option>
</select>
But at the moment this is not functional - the default php retreival script is by keyword, and i don't know how to set a different search criteria depending on the option the user selected.
Thanks for your time.
danswebs
08-27-2003, 10:47 AM
First, change the "named=" attribute of the options to "value=".
Once the user posts to a php page you will need to look for the http_post_vars (could also use get and get vars, but try to stay away from blanket var recognition). Find the one named "SearchType" and get its value. Then do a conditional statement based upon that value.
~Dan:
moondance
08-27-2003, 03:13 PM
Is http_post_vars the same as $_POST - in this case...
$_POST['SearchType']
...?
And by conditional statement do you mean something along the lines of (if the above line is correct):
if ($_POST['SearchType'] = "ID" ){
---retreive using ID as criteria---
}
if ($_POST['SearchType'] = "Keyword"){
---retreive using Keyword as criteria----
}
Thanks
;)
$HTTP_POST_VARS was depreciated in favor of $_POST in PHP version 4.1.0. So, if you are using PHP 4.1.0 or later, use $_POST
Also, you need to give your <options> values, rather than names, like this:
<select name="SearchType">
<option>Search by....</option>
<option value="ID">ID</option>
<option value="Keyword">Keyword</option>
</select>
And then, you logic is a bit off with the if statement. You need to use a == when compairing items:
if ($_POST['SearchType'] == "ID" ){
#---retreive using ID as criteria---
}
if ($_POST['SearchType'] == "Keyword"){
#---retreive using Keyword as criteria----
}
else {
#what are you going to do if they leave the "SearchType" set to the original "Search by..." option?
}
danswebs
08-28-2003, 07:32 AM
Thanks for pointing that out PYRO. $_POST is indeed the most up to date way to script this.
Anyone reading this can find out about this topic at:
http://us2.php.net/manual/en/reserved.variables.php#reserved.variables.post
Information on the rest of the PHP predefined variables can be found on the same page.
Cheers,
~Dan