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 Quote Link to comment 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. Quote Link to comment 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> Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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>'; Quote Link to comment 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. 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.