sean04 Posted March 30, 2010 Share Posted March 30, 2010 Hey! So I have a register form and there are a few drop down lists on it. If a user selects a month for example (say June) and hits the register button, if there are any errors on the page when the page validates the drop down lists will go back to its original selection value (January). What is the easiest way to keep a users selection even if the page is submitted or refreshed? Hope thats clear Thanks, Sean Link to comment https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/ Share on other sites More sharing options...
ram4nd Posted March 30, 2010 Share Posted March 30, 2010 I don't know about refresh, but you can read the values in with php after submit and then put the values to value field. Link to comment https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/#findComment-1033945 Share on other sites More sharing options...
sean04 Posted March 30, 2010 Author Share Posted March 30, 2010 Sorry I meant submit This is what I have now. <p <?php if(in_array("Month", $errors)){echo 'class="error"';} ?>> <label for="Month">Birthday:</label> <select name="Month" value="<?php echo $Month ?>"> <option value="">Choose...</option> <?PHP foreach ($months as $value) { echo '<option value="'.$value.'">'.$value.'</option>'; } ?> </select> </p> As of now, if choose is selected when the user hits submit, there will be an error telling the user to choose a month. If the user chooses a month and hits submit but there is another error, the value choose will automatically be the selected value in the drop down list once again. I have done something like this but it doesn't seem to work right. <p <?php if(in_array("Month", $errors)){echo 'class="error"';} ?>> <label for="Month">Birthday:</label> <select name="Month" value="<?php echo $Month ?>"> <option value="<?php $_POST['Month']; ?>">Choose...</option> <?PHP foreach ($months as $value) { echo '<option value="'.$value.'">'.$value.'</option>'; } ?> </select> </p> Link to comment https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/#findComment-1033948 Share on other sites More sharing options...
Rustywolf Posted March 30, 2010 Share Posted March 30, 2010 <p <?php if(in_array("Month", $errors)){echo 'class="error"';} ?>> <label for="Month">Birthday:</label> <select name="Month" value="<?php echo $Month ?>"> <?PHP foreach ($months as $value) { if($_POST['month'] == $months) echo ''<option value="'.$value.'" selected>'.$value.'</option>'; else echo '<option value="'.$value.'">'.$value.'</option>'; } ?> </select> </p> Should work? Link to comment https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/#findComment-1033954 Share on other sites More sharing options...
sean04 Posted March 30, 2010 Author Share Posted March 30, 2010 Thanks! I'll give this a try when I get home. A question though... Where would I put "Choose"? I want the application to error when the selection is "Choose" but anything else (like Jan to Dec) is valid. Link to comment https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/#findComment-1034079 Share on other sites More sharing options...
sean04 Posted March 30, 2010 Author Share Posted March 30, 2010 Ah ok I see what you did I'll try it later on! Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/#findComment-1034094 Share on other sites More sharing options...
sean04 Posted April 1, 2010 Author Share Posted April 1, 2010 Hmm... that doesn't seem to work. It still goes to its original value. Any other ideas? Thanks! Sean. Link to comment https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/#findComment-1035082 Share on other sites More sharing options...
ram4nd Posted April 1, 2010 Share Posted April 1, 2010 Your code is horrible, mixing html and php like that. if($_POST['month'] == $months) - $value instead of months Link to comment https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/#findComment-1035377 Share on other sites More sharing options...
sean04 Posted April 1, 2010 Author Share Posted April 1, 2010 I know its not the greatest... Another thing, $months is an array of all the months so I don't think replacing it with $value is an option. Thanks! Sean Link to comment https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/#findComment-1035458 Share on other sites More sharing options...
ialsoagree Posted April 1, 2010 Share Posted April 1, 2010 I know its not the greatest... Another thing, $months is an array of all the months so I don't think replacing it with $value is an option. Thanks! Sean Look at your code carefully, replacing it with $value is the ONLY option: foreach ($months as $value) { if($_POST['month'] == $months) echo ''<option value="'.$value.'" selected>'.$value.'</option>'; else echo '<option value="'.$value.'">'.$value.'</option>'; The "foreach" defines $value as the first index of $months (and then loops through each index 1 by 1 redefining $value as it does). foreach ($months as $value) { if($_POST['month'] == $value) echo ''<option value="'.$value.'" selected>'.$value.'</option>'; else echo '<option value="'.$value.'">'.$value.'</option>'; Link to comment https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/#findComment-1035461 Share on other sites More sharing options...
sean04 Posted April 5, 2010 Author Share Posted April 5, 2010 That still doesn't seem to work... I'm not sure why I can't retain values after submitting a form. Here I will post what I have again. $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'); <p <?php if(in_array("Month", $errors)){echo 'class="error"';} ?>> <label for="Month">Birthday:</label> <select name="Month" value="<?php echo $Month ?>"> <option value="">Choose...</option> <?PHP foreach ($months as $value) { echo '<option value="'.$value.'">'.$value.'</option>'; } ?> </select> </p> After submit as of now, the default drop down values will just appear.. not what the user previously chose. Link to comment https://forums.phpfreaks.com/topic/196948-keep-drop-down-value-on-refresh/#findComment-1037257 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.