Can't seem to capture a variable in a chained select

Posted on 16th Feb 2014 by admin

I'm *this* close to having a chained select running but for some reason it doesn't seem to be picking up a variable.

Code: <?php

require ('inc/connection.php');

//seeming that we are just submitting and refreshing to the one page we need to check if the post variable is set, and if so a couple of other variables are set
if(!isset($_POST['state'])) {
$next_dropdown = 0;
}
else {
//When set this variable reveals the next drop down menu
$next_dropdown = 1;
//this variable keeps the previous selection selected
$selected = $_POST['state'];
}
?>

<form name="form" method="post" action="">
<select name="state" style="font-size:20px;">
<option value="NULL">State</option>
<?php
$query = "SELECT id, name FROM state ORDER BY name ASC";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{?>
<option value="<?php echo $row[0]; ?>" onClick="document.form.submit()" <?php if(isset($selected) && $row[0] == $selected) {echo "selected='selected'";} ?>><?php echo $row[1]; ?></option>n";
<?php }
echo '</form>n';
//this is where the other form will appear if the previous form is submitted
if($next_dropdown == 1) {?>
<form name="form2" action="" method="post">
<select name="city">
<option value="NULL">City</option>
<?php
$query2 = "SELECT * FROM city WHERE state_id = " . $row[0];
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2))
{ ?>
<option value="<?php echo $row2[0]; ?>" onClick="document.form2.submit()"><?php echo $row2[1]; ?></option>
<?php }?>
</select>
</form>
<?php }
?>

The state drop down works fine. Once a state is selected, it will display the city drop down. However, the city drop down never populates. It's as though it forgets what $row[0] is. Any thoughts?

Other forums