hi, im new to php, and have this problem with a dynamic drop down box in my search page. basically it is working as it should when I select different values, however when I click submit and the page passes the select name value 'cat' to the results.php page which processes the input from the search page, it is receiving 'cat' as the index which was selected in the first drop down box, e.g. If I select "Audi" (which is the first value in the drop box and the Mysql db), it will pass "1" as 'cat' to the results.php page.
How do I make the select name 'cat' equal the actual value in the drop box instead of the index which it is in the drop box?
Any help would be much appreciated.
Many Thanks
<?php
$dbservertype='mysql';
$servername='localhost';
$dbusername='minesh';
$dbpassword='monkey';
$dbname='carwebsite';
connecttodb($servername,$dbname,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbuser,$dbpassword)
{
global $link;
$link=mysql_connect ("$servername","$dbuser","$dbpassword");
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
}
?>
<html>
<head>
<title>Car Search</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='search.php?cat=' + val ;
}
</script>
</head>
<body>
<h1>Car Search</h1>
<?
$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category");
$cat=$HTTP_GET_VARS['cat'];
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory where cat_id=$cat order by subcategory");
}else{$quer=mysql_query("SELECT DISTINCT subcategory FROM subcategory order by subcategory"); }
echo "<form method=post name=f1 action='results.php'>";
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['cat_id']==@$cat){echo "<option selected value='$noticia2[cat_id]'>$noticia2[category]</option>"."<BR>";}
else{echo "<option value='$noticia2[cat_id]'>$noticia2[category]</option>";}
}
echo "</select>";
echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[subcategory]'>$noticia[subcategory]</option>";
}
echo "</select>";
echo "<input type=submit value=Submit>";
echo "</form>";
?>
</form>
</body>
</html>