SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile 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:



    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>'; 

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    however i encounter a problem when i try to apply the script to a multi select options menu
    What is the problem?

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Welcome to Sitepoint andreea115.

    PHP Code:
    <?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.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •