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
https://forums.phpfreaks.com/topic/77451-solved-dynamic-dropdown-list-problems/
Share on other sites

You will avoid complications and the annoyance of page reloading by using AJAX which is a combination of JavaScript, XMLhttp, and PHP.  It's easier than it sounds...take a look at this article...

 

http://www.phpfreaks.com/forums/index.php/topic,167802.0.html

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.