Jump to content

need help with php drop down list


Go to solution Solved by Ch0cu3r,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/282636-need-help-with-php-drop-down-list/
Share on other sites

  • Solution

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.