Connecting Tech Pros Worldwide Help | Site Map

Load database values to drop down

Familiar Sight
 
Join Date: Jan 2008
Posts: 198
#1: Mar 26 '08
i have two drop down boxes and when user select District from 1st drop down box , the cityis in that district should load to the second drop down.
My code is below. But it is not working. Please tell me what is incorrect with it.

[PHP] <tr>
<td width="111">
<form action="test.php" method="post" name="test" >
<select name="District" class="defaultBlackText" onchange="this.form.submit();">
<option <?php if ($_POST['District'] == 'G1') print 'selected '; ?> value="G1">G1</option>
<option <?php if ($_POST['District'] == 'G2') print 'selected '; ?> value="G2">G2</option>
<option <?php if ($_POST['District'] == 'G3') print 'selected '; ?> value="G3">Kurunegala</option>
</select>
</form>
</td>
<td width="15">&nbsp;</td>
<td width="145">
<form action="test.php" method="post" name="testl">
<select name="city" onchange="this.form.submit();">
<option value="Select">Select</option>
</select>
</form>
</td>
<td width="138"><?php if (isset($_POST['District']))
{
echo $_POST['District'];
$District=$_POST['District'];
$q="SELECT * FROM xxx WHERE District='".$District."' ";
$result=mysql_query($q) or die ("Could not execute query: $q.".mysql_error());
while($row=mysql_fetch_array($result)){
foreach( $row AS $key => $val ){
$key = stripslashes( $val );
}

}
}

?> </td>
<td width="158">
</td>
<td width="12">&nbsp;</td>
<td width="190"></td>
<td>&nbsp;</td>
</tr>[/PHP]
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 27 '08

re: Load database values to drop down


No wonder it is not working and, by the way, you did not show all your code.

Secondly the only 2nd drop down box on this form is this one[php] <select name="city" onchange="this.form.submit();">
<option value="Select">Select</option>
</select>[/php]but you don't fill it anywhere, as you should between the <option> and the </select>. I cannot understand the meaning of the filling of a $key variable in the foreach loop. What is its purpose?

Ronald
Familiar Sight
 
Join Date: Jan 2008
Posts: 198
#3: Mar 27 '08

re: Load database values to drop down


Quote:

Originally Posted by ronverdonk

No wonder it is not working and, by the way, you did not show all your code.

Secondly the only 2nd drop down box on this form is this one[php] <select name="city" onchange="this.form.submit();">
<option value="Select">Select</option>
</select>[/php]but you don't fill it anywhere, as you should between the <option> and the </select>. I cannot understand the meaning of the filling of a $key variable in the foreach loop. What is its purpose?

Ronald


I want to take database value using it and want to load database values to second dropdown box.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Mar 27 '08

re: Load database values to drop down


Quote:

Originally Posted by ghjk

I want to take database value using it and want to load database values to second dropdown box.

Yes, I understood that already. But, as you said in your first post, you want to know what is wrong with it.

I gave you an answer but you don't acknowledge that answer, you just repeat your original question. Look at my post and see what is wrong.

Ronald
Reply