Linked dropdowns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
r100per
Forum Newbie
Posts: 5
Joined: Wed Sep 23, 2009 4:46 am

Linked dropdowns

Post by r100per »

Hi there,

Im trying to create a set of linked drop down menus which the seconds and third draw there values dynamically from a database and each depend on what was selected in the previous dropdown, now i have some code (see below) that when the page loads each drop down had all of there values in them but when the first is selected the second becomes blank, i cannot for the life of me work out why,

if anyone can see the error or has any points please let us know

thanks in advance

r100per

<title>linked selects</title>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='test2.php?cat=' + val ;
}
function reload3(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
var val2=form.subcat.options.value;

self.location='test2.php?cat=' + val + '&subcat=' + val2 ;
}

</script>

</head>
<?php

include "includes/connect.php";

$cat=$_GET['cat'];
$subcat=$_GET['subcat'];
$subcat2=$_GET['subcat2'];

/////////////////1st/////////////////////
$query= "SELECT * FROM cat";
$result = mysql_query($query);
/////////////////2nd/////////////////////

if (strlen($cat) > 0){
$query2= "SELECT * FROM subcat WHERE category = $cat"; }
else {
$query2= "SELECT * FROM subcat"; }

$result2 = mysql_query($query2);


////////////////3rd/////////////////////

if (strlen($subcat) > 0){
$query3= "SELECT * FROM subcat2 WHERE category = $cat AND subcategory = $subcat"; }
else {
$query3= "SELECT * FROM subcat2"; }

$result3 = mysql_query($query3);

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($row = mysql_fetch_assoc($result))
{
$catres = $row["category"];

if($catres==@$cat)
{

echo "<option selected value='$catres'>$catres</option>"."<BR>";

}

else{

echo "<option value='$catres'>$catres</option>";

}

}
echo "</select>";

/////////////////////////////////////////////////////////////
///////////////////Starting of Second drop downlist /////////

echo "<select name='subcat' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";

while($row2 = mysql_fetch_assoc($result2))
{
$subcatres = $row2["subcategory"];

if($subcatres==@$subcat)
{

echo "<option selected value='$subcatres'>$subcatres</option>"."<BR>";

}

else{

echo "<option value='$subcatres'>$subcatres</option>";

}

}
echo "</select>";

/////////////////////////////////////////////////////////////
///////////////////Starting of Third drop downlist /////////
echo "<select name='subcat2' ><option value=''>Select one</option>";

while($row3 = mysql_fetch_assoc($result3)) {

$subcat2res = $row3["subcategory2"];

echo "<option value='$subcat2res'>$subcat2res</option>";

}
echo "</select>";
echo "</form>";

?>
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Linked dropdowns

Post by papa »

You need to use hidden values in the previous select box form.
r100per
Forum Newbie
Posts: 5
Joined: Wed Sep 23, 2009 4:46 am

Re: Linked dropdowns

Post by r100per »

WHAT DO YOU MEAN BY HIDDEN VALUES?

THANKS
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Linked dropdowns

Post by papa »

Code: Select all

 
echo "<form method=post name=f1 action='dd3ck.php'>";
 
echo "<input type=\"hidden\" name=\"cat\" value=\"$cat\">";
Something like that for each select element you have. Otherwise your values will reset each time you select another dropdown.
Post Reply