Jump to content

need help with php drop down list


pxxb

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.