tgeorge06 Posted August 10, 2010 Share Posted August 10, 2010 what I am trying to accomplish is for the user to select the state they wish, hit submit. At this point another form should show asking which county based on the state they picked, then hit search. At this point, I'm having an issue having the state variable being passed to the 2nd form also having the state they selected stay selected. <?php $default = "Step 1. Pick Your State"; $select = "<option name='statebox'>$default</option>"; echo "<br/><form method='POST' action=".$_SERVER['PHP_SELF']." >"; echo "<select name='search'>"; echo "$select"; /////////////////////////////////////////////////////////////// //Connect to the database include_once 'phpforms/connect.php'; $sql = mysql_query("SELECT * FROM states"); while($row = mysql_fetch_array($sql)){ $state = $row['states']; echo "<option name='statebox'>$state</option>"; }//End While /////////////////////////////////////////////////////////////// echo "</select>"; echo "<input align='left' type='submit' name='stateboxbutton' value='Ok'> "; echo "</form>"; echo "</td>"; echo "<td>"; if(isset($_POST['stateboxbutton'])){ $statesearch = $_POST['statebox']; $selected = $_POST['statebox']; $select = "<option name='statebox'>$selected</option>"; echo "<br/><form action='../search-results.php' method='POST'>"; echo "<select name='search'>"; echo "<option name='default'>Step 2. Pick Your County</option>"; /////////////////////////////////////////////////////////////// //Connect to the database include_once 'phpforms/connect.php'; $sql = mysql_query("SELECT * FROM counties WHERE state LIKE '$statesearch'"); while($row = mysql_fetch_array($sql)){ $co = $row['counties']; echo "<option name='county'>$co</option>"; }//End While /////////////////////////////////////////////////////////////// echo "</select>"; echo "<input align='left' type='submit' name='button' value='Search'> "; echo "</form>"; }else{ }//End Else ?> Quote Link to comment https://forums.phpfreaks.com/topic/210344-post-form-same-page-define-variables/ Share on other sites More sharing options...
wildteen88 Posted August 10, 2010 Share Posted August 10, 2010 When using drop down lists you only give the <select> tag a name. But for each <option> you'll assign their value. Example dropdown list <form action="" method="post"> <select name="list_name"> <option value="option1">Option One</option> <option value="option2">Option Two</option> <option value="option3">Option Three</option> </select> <input type="submit" name="submit" value="Submit" /> </form> Notice how each option has a unique value assigned. Now when the form is submitted $_POST['list_name'] will contain the selected value. So to correct your code, change while($row = mysql_fetch_array($sql)){ $state = $row['states']; echo "<option name='statebox'>$state</option>"; }//End While To while($row = mysql_fetch_array($sql)){ $state = $row['states']; echo "<option value=\"$state\">$state</option>"; }//End While Next change $statesearch = $_POST['statebox']; to $statesearch = $_POST['search']; The next two lines can be remove, they are not necessary. $selected = $_POST['statebox']; $select = "<option name='statebox'>$selected</option>"; On your second form you'll want to pass the selected state as a hidden value echo "</select>"; echo "<input type='hidden' name='statesearch' value='$statesearch'> "; echo "<input align='left' type='submit' name='button' value='Search'> "; Quote Link to comment https://forums.phpfreaks.com/topic/210344-post-form-same-page-define-variables/#findComment-1097694 Share on other sites More sharing options...
tgeorge06 Posted August 10, 2010 Author Share Posted August 10, 2010 Made all of your changes, works great thanks! One question, I'd like to understand why you used the slashes echo "<option value=\"$state\">$state</option>"; And one more thing, How do I retain the state I selected in the first dropdown after the submit button has been pressed? Quote Link to comment https://forums.phpfreaks.com/topic/210344-post-form-same-page-define-variables/#findComment-1097698 Share on other sites More sharing options...
wildteen88 Posted August 10, 2010 Share Posted August 10, 2010 I used the backslash for escaping the (double) quote echo "<option value=\"$state\">$state</option>"; If you remove the slash PHP will report a syntax error, due the string being defined prematurely. Quote Link to comment https://forums.phpfreaks.com/topic/210344-post-form-same-page-define-variables/#findComment-1097700 Share on other sites More sharing options...
tgeorge06 Posted August 10, 2010 Author Share Posted August 10, 2010 oh ok, i usually just use single quotes... should i \" from now on? Quote Link to comment https://forums.phpfreaks.com/topic/210344-post-form-same-page-define-variables/#findComment-1097701 Share on other sites More sharing options...
wildteen88 Posted August 10, 2010 Share Posted August 10, 2010 There is no right or wrong way. Its down to personal preference really. I'm just used to wrapping HTML attribute values within double quotes. Quote Link to comment https://forums.phpfreaks.com/topic/210344-post-form-same-page-define-variables/#findComment-1097703 Share on other sites More sharing options...
tgeorge06 Posted August 10, 2010 Author Share Posted August 10, 2010 Ok, thanks for the help...any idea how to keep the selected state in the first dropdown box/form after pressing the submit button? Quote Link to comment https://forums.phpfreaks.com/topic/210344-post-form-same-page-define-variables/#findComment-1097704 Share on other sites More sharing options...
wildteen88 Posted August 10, 2010 Share Posted August 10, 2010 Sure, Change the states while loop to while($row = mysql_fetch_array($sql)) { $state = $row['states']; $selected = (isset($_POST['search']) && $_POST['search'] == $state) ? ' selected="selected"' : ''; echo "<option value=\"$state\"$selected>$state</option>"; }//End While Quote Link to comment https://forums.phpfreaks.com/topic/210344-post-form-same-page-define-variables/#findComment-1097706 Share on other sites More sharing options...
tgeorge06 Posted August 10, 2010 Author Share Posted August 10, 2010 as simple as that.... one day, one day i will help others as you have helped me. phpfreaks recommended is right =) Marking as solved. Thank you for your time! Quote Link to comment https://forums.phpfreaks.com/topic/210344-post-form-same-page-define-variables/#findComment-1097708 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.