Load database values to drop down

Discussion in 'PHP' started by ghjk, Mar 26, 2008.

  1. #1
    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.

      <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:
     
    ghjk, Mar 26, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0