Populate 2nd dropdown based on results of 1st

snahid11
12:10:11
I found a script on this site for accomplishing the task.. However, I changed some fields to match my table and database (i'm using city and states instead of categories and subcategories)...

When I populate the 1st drop down, the field still shows 'Select One' instead of the value I selected. Though it does appear that it's registering what I selected because the URL changes pointing the the state I selected... But the second drop down does not show the cities for the state I selected.. It just shows 'Select one'... So I have 2 problems.. Cities for the state now showing up and the 1st drop down now showing the state I selected.. Below is the script I have. Any idea what I'm doing wrong!??

Thanks

-S

<?php
//***************************************
// This is downloaded from www.plus2net.com //
/// You can distribute this code with the link to www.plus2net.com ///
// Please don't remove the link to www.plus2net.com ///
// This is for your learning only not for commercial use. ///////
//The author is not responsible for any type of loss or problem or damage on using this script.//
/// You can use it at your own risk. /////
//*****************************************

$dbservertype='mysql';
$servername='localhost';
// username and password to log onto db server
$dbusername='root';
$dbpassword='';
// name of database
$dbname='Eventsdb';

////////////////////////////////////////
////// DONOT EDIT BELOW /////////
///////////////////////////////////////
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());
}
//////// End of connecting to database ////////
?>

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

<html>

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

</script>
</head>

<body>
<?

/*
If register_global is off in your server then after reloading of the page to get the value of cat from query string we have to take special

care.
To read more on register_global visit.
http://www.plus2net.com/php_tutorial/register-globals.php
*/
@$state=$_GET['state']; // Use this line or below line if register_global is off


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

///////// Getting the data from Mysql table for first list box//////////
$quer2=mysql_query("SELECT DISTINCT state, state_id FROM states order by state");
///////////// End of query for first list box////////////

/////// for second drop down list we will check if category is selected else we will display all the subcategory/////
if(isset($state) and strlen($state) > 0){
$quer=mysql_query("SELECT DISTINCT city FROM cities where state_id = $state order by cities");
}else{$quer=mysql_query("SELECT DISTINCT city FROM cities order by cities"); }
////////// end of query for second subcategory drop down list box ///////////////////////////

echo "<form method=post name=f1 action='dd-check.php'>";
/// Add your form processing page address to action in above line. Example action=dd-check.php////
////////// Starting of first drop downlist /////////
echo "<select name='state' onchange=\"reload(this.form)\"><option value=''>Select one1</option>";
while($noticia2 = mysql_fetch_array($quer2)) {
if($noticia2['state_id']==@$state){echo "<option selected value='$noticia2[state_id]'>$noticia2[state]</option>"."<BR>";}
else{echo "<option value='$noticia2[state]'>$noticia2[state]</option>";}
}
echo "</select>";
////////////////// This will end the first drop down list ///////////

////////// Starting of second drop downlist /////////
echo "<select name='city'><option value=''>Select one2</option>";
while($noticia = mysql_fetch_array($quer)) {
echo "<option value='$noticia[city]'>$noticia[city]</option>";
}
echo "</select>";
////////////////// This will end the second drop down list ///////////
//// Add your other form fields as needed here/////
echo "<input type=submit value=Submit>";
echo "</form>";
?>
<center><a href='http://www.plus2net.com'>PHP SQL HTML free tutorials and scripts</a></center>
</body>

</html>
rssridhar17
12-28-2011
Hi
This is a very useful script, I was searching all over the net, it took 1 day to get to this. Excellent, Kudos !!!!!!

My issue is, on selection of first dropdown, instead of 2nd dropdown, I want the value to be displayed in an Input field, can I do this ?

echo "<input type="text" name="subcat"
while($noticia = mysql_fetch_array($quer)) {
value="$noticia[subcategory]"}>";

This is giving Tstring error, pl let me know how do i do this.
pradeep
02-11-2012
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val ;
}
</script>
can any one please explain the complete code ?
cabalsdemon
06-09-2012
function reload(form)
this is explaining that there is a function of reloading the form but first the reload is the name that you would call in the html <select tag when you call onchange= for the javascript and the (form) is obviously the form
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
the value of the form 'cat' as in the select name that is in that form option that it will give you [the form name 'cat' that the options you will be given for the dropdown box ]
selectedIndex isThe number (base 0) of the item that is selected in the select list
self.location='dd.php?cat=' + val ;
self location is the all of the domain stuff as in where the dd.php is at ?cat is how you seperate the dd.php and make it give the information of cat and obviously the + val would be adding the value up top to the cat=

}
cabalsdemon
06-09-2012
if you are going to echo "<input type="text" name="subcat"
then you need first to echo "<input type=\"text\" name=\"subcat\"
while($noticia = mysql_fetch_array($quer)) {
value=\"$noticia[subcategory]\"}>"; //backslash the other quotaions
cabalsdemon
06-09-2012
$quer=mysql_query("SELECT DISTINCT city FROM cities where state_id = $state order by cities");

if you are going to have where state_id = $state then you need a state_id in the cities table or you can have a state data row instead of a state_id date row
Please Login to post your reply or start a new topic