Jump to content

populating dropdown help with script


dflow

Recommended Posts

i managed to get this script working

i have a Category tbl in the first dropdown

and the second dropdown needs to be selected according to CategoryID

 

the result i get is

if a categoryID=2 is selected i get:

the second dropdown is populated correctly

with the city_list

but

 

the first selected dropdown is then reset with no value/label showing

the result url parameter dd3.php?cat=2

 

then if i select the second drop down with CityID=1

it is reset

with the url parameter showing dd3.php?cat=&cat3=1(the selected CityID)

 

the idea is to continue to a third dropdown list

here is the code:

<html>

<head>

<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>
<?php
echo "<form method=post name=f1 action='dd3ck.php'>";
$quer2=mysql_query("SELECT CategoryName,CategoryID FROM category_list order by CategoryName"); 
echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['CategoryID']==@$cat){echo "<option selected value='$noticia2[CategoryID]'>$noticia2[CategoryName]</option>"."<BR>";}
else{echo  "<option value='$noticia2[CategoryID]'>$noticia2[CategoryName]</option>";}
}
echo "</select>";
//  second drop down list 
$cat=$HTTP_GET_VARS['cat'];
echo "<select name='subcat' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";
switch ($cat) {
    case 1:
$quer=mysql_query("SELECT * FROM country_list  order by CountryName");
        
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[CountryID]'>$noticia[CountryName]</option>";
}

    case 2:
        $quer=mysql_query("SELECT * FROM city_list  order by CityName");
          
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[CityID]'>$noticia[CityName]</option>";
}

    case 3:
$quer=mysql_query("SELECT * FROM country_list  order by CountryName");
        
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[CountryID]'>$noticia[CountryName]</option>";
}

}
echo "</select>";
?>

<?php




echo "<input type=submit value='Submit the form data'></form>";
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/180455-populating-dropdown-help-with-script/
Share on other sites

You don't define $cat (which you use to set the selected category option) until after you have created the category list!

 

I went ahead and cleaned up the code

 

<?php

//Get selected category
$cat = (isset($_GET['cat']) ? $_GET['cat'] : false;

//Generate category options
$catOptions = '';
$query = "SELECT CategoryName,CategoryID FROM category_list order by CategoryName";
$result = mysql_query($query); 
while($option = mysql_fetch_array($result))
{
    $selected = ($option['CategoryID']==$cat) ? ' selected="selected"': '';
    $catOptions .= "<option value=\"{$option['CategoryID']}\"{$selected}>{$option['CategoryName']}</option>\n";
}

//Generate subcategory options
$subcatOptions = '';
$query = false;
switch ($cat)
{
    case 1:
    case 3:
        $query = "SELECT CountryID as value, CountryName as text FROM country_list ORDER BY CountryName";
        break;
    case 2:
        $query = "SELECT CityID as value, CityName as text FROM city_list ORDER BY CityName";
        break;
}

if ($query!==false)
{
    $result = mysql_query($query); 
    while($option = mysql_fetch_array($result))
    {
        $subcatOptions .= "<option value=\"{$option['value']}\">{$option['text']}</option>\n";
    }
}

?>
<html>

<head>

<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>
<form method="post" name="f1" action="dd3ck.php">

<select name="cat" onchange="reload(this.form);">
  <option value="">Select one</option>
  <?php echo $catOptions; ?>
</select>

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

<input type="submit" value="Submit the form data">

</form>

</body>
</html>

As my sig states there will likely be syntax errors. I write the code with the logic in mind. Since I don't hae your database - and I'm too lazy to create one - I did not test the code.

 

Try changing this

$cat = (isset($_GET['cat']) ? $_GET['cat'] : false;

 

To this

$cat = (isset($_GET['cat'])) ? $_GET['cat'] : false;

 

There's a good chance there are other syntax errors as well.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.