Click to See Complete Forum and Search --> : Passing multiple variables through a drop down form?
hammerslane
10-23-2003, 08:40 AM
<form name="menu" method="get" action="options.php">
<option value=companyA>Company A</option>
<option value=companyB>Company B</option>
<option value=companyC>Company C</option>
<input type="submit" value="GO" name="menu">
this is my code, and as it stands, "?menu=CompanyB", if CompanyB was selected. i also want to pass the URL of companyB through the form, by selecting just one option. is this possible?
thanks
Sure, just choose a delimiter, and pass the values as normal. Then, when you read them out, just split at the delimiter, like this:
<form name="menu" method="get" action="<?PHP echo $_SERVER['PHP_SELF']; ?>">
<p><select name="companies">
<option value="companyA|http://www.companya.com">Company A</option>
<option value="companyB|http://www.companyb.com">Company B</option>
<option value="companyC|http://www.companyc.com">Company C</option>
</select>
<input type="submit" value="GO" name="menu"></p>
</form>
<?PHP
if (isset($_GET['companies'])) {
$query = $_GET['companies'];
list ($company, $url) = split("\\|", $query);
echo $company."<br>".$url;
}
?>
hammerslane
10-23-2003, 09:49 AM
thanks pyro :cool:
Sure thing, though I noticed a problem with my post (due to a formatting error by the forums) and have fixed that... :)
hammerslane
10-23-2003, 10:11 AM
ah right.. thanks.
one thing actually... i've only been doing php for a month or two, i know that || means 'or' - what does | mean? if you type | into the php.net search engine, it doesn't actually work... also whats the english word for "|" ? as in "&" is ampersand and "_" is under score... any one have any idea (pyro)?
thanks!:)
The pipe (english translation of "|" ;)) alone doesn't have any special meaning. I just chose that as the separator between the company name and the URL. We could have used any character that we didn't think would have appeared in either the company name or url, or for better reliability, we could have even used multiple characters. The trick is, when we use the split to extract the values, we need to be sure that it is only going to split at the point we want it to.