/    Sign up×
Community /Pin to ProfileBookmark

simple search using <select> box

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.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@danswebsAug 27.2003 — 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:
Copy linkTweet thisAlerts:
@moondanceauthorAug 27.2003 — 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

?
Copy linkTweet thisAlerts:
@pyroAug 27.2003 — $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:

&lt;select name="SearchType"&gt;
&lt;option&gt;Search by....&lt;/option&gt;
&lt;option value="ID"&gt;ID&lt;/option&gt;
&lt;option value="Keyword"&gt;Keyword&lt;/option&gt;
&lt;/select&gt;


And then, you logic is a bit off with the if statement. You need to use a == when compairing items:

[code=php]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?
}
[/code]
Copy linkTweet thisAlerts:
@danswebsAug 28.2003 — 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
Copy linkTweet thisAlerts:
@pyroAug 28.2003 — No problem... ?
×

Success!

Help @moondance spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...