André Tavares
André Tavares

Reputation: 45

Keep the selected options after submitting the form

Good morning, I have a problem that is: I can not keep several options selected after submitting the form and I would like someone to help me.

<select name="utilizadores[]" id="utilizadores" multiple="multiple" class="selectpicker" data-live-search="true" data-actions-box="true" title="Utilizadores">
<?php  while ($reg_sql=mysqli_fetch_array($res_sql)){?>

<option value="<?php echo $reg_sql['ID_USER']; ?>"><?php echo $reg_sql['NOMEUSER']; ?></option>
<?php } ?>  
</select>
<script type="text/javascript">
  document.getElementById('utilizadores').value = "<?php echo $_POST['utilizadores[]'];?>";
</script> 

this is my code to have the various options in the select box

Upvotes: 1

Views: 82

Answers (3)

JoeCrash
JoeCrash

Reputation: 532

An example of how you can do this. Either add the "selected" string if yes or leave blank if no. You can also write selected="selected". You can do the same thing to set disabled or readonly.

<select name="utilizadores[]" id="utilizadores" multiple="multiple" class="selectpicker" data-live-search="true" data-actions-box="true" title="Utilizadores">
<?php  while ($reg_sql=mysqli_fetch_array($res_sql)){?>
<?php $selected = isset($reg_sql["mi_variable"]) ? "selected" : ""; ?>
    <option value="<?php echo $reg_sql['ID_USER'];?>" <?php echo $selected; ?> >
        <?php echo $reg_sql['NOMEUSER']; ?>
    </option>
<?php } ?>  
</select>
<script type="text/javascript">
      document.getElementById('utilizadores').value = "<?php echo $_POST['utilizadores[]'];?>";
</script> 

Upvotes: 0

Ozal  Zarbaliyev
Ozal Zarbaliyev

Reputation: 638

You have to check if $_POST['utilizadores'] or $_GET['utilizadores'] it depends on your request type. I will use $_POST in here for explain my answer.

your select is multiple, you can use in_array function for checking that if result from db record is in array of $_POST['utilizadores']

<select name="utilizadores[]" id="utilizadores" multiple="multiple" class="selectpicker" data-live-search="true" data-actions-box="true" title="Utilizadores">

     <?php  while ($reg_sql=mysqli_fetch_array($res_sql)){?>

     **<option value="<?php echo $reg_sql['ID_USER']; ?>"
        <?php 
            if(isset($_POST['utilizadores'])){
              if(in_array($reg_sql['ID_USER'], $_POST['utilizadores'])){
                 echo 'selected';
              }else{
                echo '';
              }
           }
         ?>
     >**<?php echo 
          $reg_sql['NOMEUSER']; ?></option>
<?php } ?>  
</select>

Upvotes: 1

kchason
kchason

Reputation: 2885

You may be able to do it more efficiently if your database result also contains which rows are selected, but when you loop through, just add the selected="selected" attribute to the <option> tag.

Assuming your $_POST array exists in this scope, you can use the in_array function in PHP to determine if the option has been selected (docs).

The ternary based operation is as follows:

in_array($reg_sql['ID_USER'],$_POST['utilizadores']) ? 'selected="selected"' : ''

Which says "if the ID_USER is in the post array, then print the selected attribute, otherwise, print a blank string"

Putting it all together:

<select name="utilizadores[]" id="utilizadores" multiple="multiple" class="selectpicker" data-live-search="true" data-actions-box="true" title="Utilizadores">
    <?php  while ($reg_sql=mysqli_fetch_array($res_sql)){?>
        <option value="<?php echo $reg_sql['ID_USER']; ?>" <?= in_array($reg_sql['ID_USER'],$_POST['utilizadores']) ? 'selected="selected"' : '' $?>>
            <?php echo $reg_sql['NOMEUSER']; ?>
        </option>
<?php } ?>  
</select>

Upvotes: 0

Related Questions