pxxb Posted October 2, 2013 Share Posted October 2, 2013 Hi, I have a drop down scroll list and am trying to get it to remember the selection when I submit. Also I wan't to print an error message if they don't select any provinces or if they select a certain province. How would I do that? <legend> Preferences</legend><label for='formProvinces[]'>Province (Multiple Select) *</label><br><select size = 12 multiple="multiple" name="formProvinces[] /><?php foreach ($PROVINCES as $province){ ?><option value="US"><?php echo $province; ?></option><?php } ?></select> -------------------- i was thinking along the lines of this... if (empty($province)){ $error[] = 'Must select atleast one province'; } else if ($province[1]){ $error[] = 'Please select another province'; } how do i keep the memory of the selection after submitting? thanks Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted October 2, 2013 Solution Share Posted October 2, 2013 I'm assuming your submitting the form using POST. To preserve the selected values you'll need to see if the current option you are making exists within the $_POST['formProvinces'] array. If it is then add the selected attribute to the <option> tag. <legend> Preferences</legend> <label for='formProvinces[]'>Province (Multiple Select) *</label><br> <select size = 12 multiple="multiple" name="formProvinces[] /> <?php foreach ($PROVINCES as $province){ $selected = ''; if(isset($_POST['formProvinces']) && in_array($provinces, $_POST['formProvinces'])) $selected = ' selected'; ?> <option value="US"<?php echo $selected; ?>><?php echo $province; ?></option> <?php } ?> </select> Also I wan't to print an error message if they don't select any provinces or if they select a certain province You need to check the selected options value. The value of the selected option is what is stored in the $_POST['formProvinces'] array. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 2, 2013 Share Posted October 2, 2013 It's better if your options all have different values though. 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.