Jump to content

[SOLVED] Dynamic Dropdown List Problems


NathanS

Recommended Posts

Hi there,

 

I'm attempting to populate a second drop down list, dependent on the first. I'm running into troubles, can anyone see any blatant issues with the below code?

 

The table i'm using has three columns: "id", "abimake" and "abimodel". Idea being with the code below that when the user selects "abimake", it'll populate "abimodel" where "abimake" is what they selected.

 

Thanks!

 

<?php

$dbservertype='mysql';
$servername='localhost';

$dbusername='root';
$dbpassword='';
// name of database
$dbname='epa';


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());
}

?>

<!doctype html public "-//w3c//dtd html 3.2//en">

<html>

<head>
<title>Multiple drop down list box</title>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;
}

</script>
</head>

<body>
<?

//@$cat=$_GET['cat']; // Use this line or below line if register_global is off
@$cat=$HTTP_GET_VARS['cat']; // Use this line or above line if register_global is off


$quer2=mysql_query("SELECT DISTINCT abimake FROM test_drop"); 



//if(isset($cat) and strlen($cat) > 0){
$quer=mysql_query("SELECT DISTINCT abimodel FROM test_drop where abimake=$cat"); 
//}else{$quer=mysql_query("SELECT DISTINCT abimodel FROM test_drop order by abimodel"); } 


echo "<form method=post name=f1 action='dd-check.php'>";

echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
while($noticia2 = mysql_fetch_array($quer2)) { 
if($noticia2['abimake']==$cat){echo "<option selected value='$noticia2[abimake]'>$noticia2[abimake]</option>"."<BR>";}
else{echo  "<option value='$noticia2[abimake]'>$noticia2[abimake]</option>";}
}
echo "</select>";



echo "<select name='subcat'><option value=''>Select one</option>";
while($noticia = mysql_fetch_array($quer)) { 
echo  "<option value='$noticia[abimodel]'>$noticia[abimodel]</option>";
}
echo "</select>";


echo "<input type=submit value=Submit>";
echo "</form>";
?>

</body>

</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.