NathanS Posted November 15, 2007 Share Posted November 15, 2007 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> Quote Link to comment Share on other sites More sharing options...
axiom82 Posted November 15, 2007 Share Posted November 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.