Jump to content

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


Recommended Posts

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

 

<?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?

SELECT * FROM city WHERE state_id = " . $row[0];

were is the . "??

 

Are u sure it's even gathering the mysql data from that query? i would put a error function in it to see.

 

For now i dont see why it's not catching the data and populating the list, just that query looks weird w/o proper '. .' or ". ."

 

 

 

Try just taking out the ' and . and just use the normal variable .. i mean iuno just guess and check bro.. looks right.. sorry maybe other fortunate helpers might help u.

 

Keep your code organized and it is much easier to work with and debug. This is not tested

 

<?php

require ('inc/connection.php');

//Get selected state if set
$selectedState = (isset($_POST['state'])) ? $_POST['state'] : false;

//Create the State options
$stateOptions = "<option value=\"\">State</option>\n";
$query = "SELECT id, name FROM state ORDER BY name ASC";
$result = mysql_query($query);        
while($row = mysql_fetch_array($result))
{
    $selected = ($selectedState && $row[0]==$selectedState) ? ' selected="selected"' : '';
    $stateOptions .= "<option value=\"{$row[0]}\" onClick=\"this.form.submit();\"{$selected}>{$row[1]}</option>\n";
}

//Create the City options
if ($selectedState===false)
{
    $cityOptions = "<option value=\"\">Select a state</option>\n";
}
else
{
    $cityOptions = "<option value=\"\">Select a state</option>\n";
    $query = "SELECT * FROM city WHERE state_id = {$selectedState}";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result))
    {
        $stateOptions .= "<option value=\"{$row[0]}\" onClick=\"this.form.submit();\">{$row[1]}</option>\n";
    }
}

?>

<form name="form" method="post" action="">
    <select name="state" style="font-size:20px;">
        <?php echo $stateOptions; ?>
    </select>
</form>

<form name="form2" action="" method="post">
    <select name="city">
        <?php echo $cityOptions; ?>
    </select>
</form>

Thanks for the rewrite.  It's kicking this error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /path/to/file.php on line 28

 

Which is here:

 

    while($row = mysql_fetch_array($result))
    {
        $stateOptions .= "<option value=\"{$row[0]}\" onClick=\"this.form.submit();\">{$row[1]}</option>\n";
    }

Then the query is failing. As my signature states I don't always test my code - especially when the code requires someone else's database.

 

On the line above, change this

    $result = mysql_query($query);

To this:

    $result = mysql_query($query) or die("$query <br><br>".mysql_error());

That should identify the problem.

 

Also, to prevent sql injection, change this

$selectedState = (isset($_POST['state'])) ? $_POST['state'] : false;

To This

$selectedState = (isset($_POST['state'])) ? mysql_real_escape_string($_POST['state']) : false;

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.