How to i make a sticky form out of a multi selection option

Hello everyone,

i am new to PHP and trying to write my first script.

I have scripted a drop down menu for a sticky form. the script works

however i encounter a problem when i try to apply the script to a multi select options menu .

i guess the solution will be to loop around the
($key == $sticky)
but i am not sure the best way to do this.

i think your kind response and assistance in advance.

i have enclosed two script.
Script One
the faulty script (i.e the multi select script)

    



PHP Code:
    $selection =array ( 

 '72'=>'Afrikaans', '1'=>'Albanian ' ,'‘2'=>'Arabic' ,'3'=>'Armenian','4'=>'ASL Sign Language' ,'5'=>'Bahasa Malaysia' , 
    '6'=>'Bangala ' ,'7'=>'Bavarian' ,'8'=>'Bengali' ,'9'=>'Bosnian' ,'10'=>'Braille' ,'11'=>'Bulgarian ') 

     
     
    $sticky = ''; 
    if(isset($_POST['submit'])) { 
    $sticky = $_POST['visa_held[]']; 
     
    } 
     

     
    echo '<label for="nationality" class="req">   
     <select multiple name="visa_held[]" size="6" style="font-family: Tahoma; font-size: 10pt">     

      
         </label>';   
     


     
    echo '<option value="NULL">Select One or More</option>'; 
    echo  '<option value="US" > United States</option>'; 
     
    value="SP">Spain</option>'; 
    echo  '    <option value="WU">Western Europe</option>'; 

     
     
    echo  '<option value="">---------------------</option>';  

     
    foreach ($selection as $key => $value){ 
     
    if($key == $sticky) { 
    echo '<option value="' . $key .'" selected>' . $value . '</option>'; 
    }else{ 
    echo '<option value="' . $key .'">' . $value . '</option>'; 
    } 
     
     
    } 

     echo'</select>;  
    ?>  
The script that works 

PHP Code:



        
        if(isset($_POST['submit']) && !$CL) 
  echo '<p><font color="red" size="+1"><b>!Please tell us what Country you live in now.</b></font><br /></p>'; 
  else 
  echo '<p> <label> What country do you live in now?<span>*</span> </label></p>'   
   
   
      
          
     
    $selection =array ( 

 '72'=>'Afrikaans', '1'=>'Albanian ' ,'‘2'=>'Arabic' ,'3'=>'Armenian','4'=>'ASL Sign Language' ,'5'=>'Bahasa Malaysia' , 
    '6'=>'Bangala ' ,'7'=>'Bavarian' ,'8'=>'Bengali' ,'9'=>'Bosnian' ,'10'=>'Braille' ,'11'=>'Bulgarian ') 


     
    $sticky = ''; 
    if(isset($_POST['submit'])) { 
    $sticky = $_POST['country_loc']; 
     
    } 
     

     
    echo '<label for="country_loc" class="req"></label> 
      
      
         <select name="country_loc" id="country_loc" class="required"  
     
     />';   
     
     
    echo '<option value="NULL">Select One</option>'; 
     
    echo  '<option value="">---------------------</option>';  

     
     
    foreach ($selection as $key => $value){ 
     
    if($key == $sticky) { 
    echo '<option value="' . $key .'" selected>' . $value . '</option>'; 
    }else{ 
    echo '<option value="' . $key .'">' . $value . '</option>'; 
    } 
     
     
    } 

     echo'</select>';  
   


however i encounter a problem when i try to apply the script to a multi select options menu

What is the problem?

Welcome to Sitepoint andreea115. :wink:


<?php
$options = array(
  'us'  => 'United States',
  'uk'  => 'United Kingdom',
  'it'  => 'Italy',
  'es'  => 'Spain'
);

function selected($code){
  
  if(empty($_POST['countries'])){
    return;
  }
  
  if(!is_array($_POST['countries'])){
    return;
  }
  
  if(!in_array($code, $_POST['countries'])){
    return;
  }
  
  return 'selected="selected"';
}

?>
<form action="#" method="post">
  <select name="countries[]" multiple="multiple">
    <?php foreach($options as $code => $text): ?>
      <option value="<?php echo $code; ?>" <?php echo selected($code); ?>>
        <?php echo $text; ?>
      </option>
    <?php endforeach; ?>
  </select>
  <input type="submit" value="submit" />
</form>

Let us know if you have any questions. :slight_smile:

What is a “sticky form”? I’ve not come across that term before.

(have mental image of a normal form but with lots of fluff and dust stuck to it)