Page 1 of 1

Dropdown Refresh problem!

Posted: Wed May 28, 2008 7:26 am
by JimiH
Hello I have the following code that displays three drop down boxes linked to a mysql db. The second
dropdown is based on the first dropdown selected value etc etc.

The problem I am having is when I select the first value the page refreshes and I'm left with "Select One" again.

Example of correctly working dropdowns here

http://www.plus2net.com/php_tutorial/dd3.php

Code: Select all

<!doctype html public "-//w3c//dtd html 3.2//en">
 
<html>
 
<head>
<title>Demo of Three Multiple drop down list box from plus2net</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='dd3.php?cat=' + val ;
}
function reload3(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value; 
var val2=form.subcat.options[form.subcat.options.selectedIndex].value; 
 
self.location='dd3.php?cat=' + val + '&cat3=' + val2 ;
}
 
</script>
</head>
 
<body>

Code: Select all

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT category,cat_id FROM category order by category"); 
///////////// End of query for first list box////////////
 
/////// for second drop down list we will check if category is selected else we will display all the subcategory///// 
$cat=$HTTP_GET_VARS['cat']; // This line is added to take care if your global variable is off
if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT subcategory,subcat_id FROM subcategory where cat_id=$cat order by subcategory"); 
}else{$quer=mysql_query("SELECT DISTINCT subcategory,subcat_id FROM subcategory order by subcategory"); } 
////////// end of query for second subcategory drop down list box ///////////////////////////
 
 
/////// for Third drop down list we will check if sub category is selected else we will display all the subcategory3///// 
$cat3=$HTTP_GET_VARS['cat3']; // This line is added to take care if your global variable is off
if(isset($cat3) and strlen($cat3) > 0){
$quer3=mysql_query("SELECT DISTINCT subcat2 FROM subcategory2 where subcat_id=$cat3 order by subcat2"); 
}else{$quer3=mysql_query("SELECT DISTINCT subcat2 FROM subcategory2 order by subcat2"); } 
////////// end of query for third subcategory drop down list box ///////////////////////////
 
 
echo "<form method=post name=f1 action='dd3ck.php'>";
//////////        Starting of first drop downlist /////////
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>";
//////////////////  This will end the first drop down list ///////////
 
//////////        Starting of second drop downlist /////////
echo "<select name='subcat' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) { 
if($noticia['subcat_id']==@$cat3){echo "<option selected value='$noticia[subcat_id]'>$noticia[subcategory]</option>"."<BR>";}
else{echo  "<option value='$noticia[subcat_id]'>$noticia[subcategory]</option>";}
}
echo "</select>";
//////////////////  This will end the second drop down list ///////////
 
 
//////////        Starting of third drop downlist /////////
echo "<select name='subcat3' ><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer3)) { 
echo  "<option value='$noticia[subcat2]'>$noticia[subcat2]</option>";
}
echo "</select>";
//////////////////  This will end the third drop down list ///////////
 
 
echo "<input type=submit value='Submit the form data'></form>";
Thanks

Geoff

Re: Dropdown Refresh problem!

Posted: Thu May 29, 2008 5:41 am
by JimiH
Think it may be something to do with this section

Code: Select all

//////////        Starting of first drop downlist /////////
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>";
//////////////////  This will end the first drop down list ///////////
 
Thanks

Geoff

Re: Dropdown Refresh problem!

Posted: Mon Jun 02, 2008 9:19 am
by JimiH
Ok, solved

If anyone else has this problem the fix was to change

Code: Select all

$cat=$HTTP_GET_VARS['cat'];
to

Code: Select all

$cat=$_GET['cat'];
Not to sure why it worked but it does.

thanks

Geoff

Re: Dropdown Refresh problem!

Posted: Mon Jun 02, 2008 10:02 am
by timsewell
Maybe there are reasons why you don't want to, but a dash of AJAX would remove the need for the page refreshes. This chap here has a very simple implementation using JQuery.

http://remysharp.com/2007/01/20/auto-po ... query-ajax