killdozer Posted April 10, 2011 Share Posted April 10, 2011 I'm stuck trying to figure out why when I make a selection in drop down list of a php form, the selection does not stay when form is refreshed or re-displayed due to an pattern error of another field. My array is state_province = array(list of the states, and provinces) field name in the label array is: "state" => "State", Here is the code that I'm using: echo "<form action='$_SERVER[php_SELF]' method='POST'>"; foreach($labels as $field => $label) { if($field == "state") { echo "<div class='province_state'><label for='state' size='15'>* Province/State</label><select>"; foreach($state_province as $state) { echo "<option value=$state"; if(@$_POST['state'] == $state) { echo "selected='selected'"; } echo ">"; echo $state; echo "</option>\n"; } echo "</select></div>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/233241-debug-code-for-retaining-selection/ Share on other sites More sharing options...
dcro2 Posted April 10, 2011 Share Posted April 10, 2011 Your <select> doesn't have a name. Unless you make the <label> end after </select>, you'll need to set its name. Quote Link to comment https://forums.phpfreaks.com/topic/233241-debug-code-for-retaining-selection/#findComment-1199519 Share on other sites More sharing options...
killdozer Posted April 10, 2011 Author Share Posted April 10, 2011 Ok I changed it to this, but no change for the retention: echo "<form action='$_SERVER[php_SELF]' method='POST'>"; foreach($labels as $field => $label) { if($field == "state") { echo "<div class='province_state'><label for='state' size='15'>* Province/State</label><select name='state' id='state'>"; foreach($state_province as $state) { echo "<option value= $state"; if(@$_POST['state'] == $state) { echo "selected='selected'"; } echo ">"; echo $state; echo "</option>\n"; } echo "</select></div>\n"; } Your <select> doesn't have a name. Unless you make the <label> end after </select>, you'll need to set its name. Quote Link to comment https://forums.phpfreaks.com/topic/233241-debug-code-for-retaining-selection/#findComment-1199522 Share on other sites More sharing options...
dcro2 Posted April 10, 2011 Share Posted April 10, 2011 Sorry, I guess I forgot to mention, you have a space between the value attribute and $state, so to the browser that means it has no value and that you have an invalid attribute called $state. It should also be in quotes. echo "<option value='$state'"; Quote Link to comment https://forums.phpfreaks.com/topic/233241-debug-code-for-retaining-selection/#findComment-1199543 Share on other sites More sharing options...
killdozer Posted April 10, 2011 Author Share Posted April 10, 2011 Thanks ever so much dcro2, I have spent over a week, trying various solutions, sometimes you cannot see the forest for the trees, but I never knew about adding the select id. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/233241-debug-code-for-retaining-selection/#findComment-1199653 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.