86Stang Posted November 11, 2009 Share Posted November 11, 2009 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? Quote Link to comment Share on other sites More sharing options...
Gayner Posted November 11, 2009 Share Posted November 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
86Stang Posted November 11, 2009 Author Share Posted November 11, 2009 I don't need the . " but I added it anyway and it doesn't change anything. No errors but it doesn't seem to be gathering the data. Any thoughts on why? Quote Link to comment Share on other sites More sharing options...
Gayner Posted November 11, 2009 Share Posted November 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 11, 2009 Share Posted November 11, 2009 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> Quote Link to comment Share on other sites More sharing options...
86Stang Posted November 11, 2009 Author Share Posted November 11, 2009 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"; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 11, 2009 Share Posted November 11, 2009 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; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.