SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast adamschroeder's Avatar
    Join Date
    Mar 2011
    Location
    World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    multiple select box

    Hi guys,
    I have this multiple select box I've been working on for 5 hours now and I got everything to work except for one thing.
    I can't seem to get the chosen values to retain in the select box after form is submitted.
    I tried looking for the answer on the web but the examples they give there are too complicated. I also tried using the foreach function but that only echos out the chosen countries without selecting them.

    Can someone give me advice on how to do this please?

    The code:
    PHP Code:
    <form action="register-me.php" method="post">
    <select name="inputcountry[]" class="month" size=3 multiple>
    <?php
    $countries
    =$_POST['inputcountry'];
    foreach (
    $countries as $country)
    echo 
    "<option>$country</option>";
    ?>
    <option value="Europe" class="one">Europe</option>
    <option value="---">--</option>
    <option value="Albania">Albania</option>
    <option value="Andorra">Andorra</option>
    <option value="Austria">Austria</option>
    <selected>

  2. #2
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to add the selected="selected" to the options.

    Here is a quick example:
    PHP Code:
    <?php 
       $countryArray 
    = array("Alaska""Arkansas""Arizona""Argentina""Brazil""Chile");
    ?>
    <form action="#" method="post">
        <select name="multi[]" size="3" multiple>
        <?php
        
            
    foreach( $countryArray as $c ) {
                
    $selected in_array($c$_POST['multi'])
                    ? 
    ' selected="selected"'
                    
    '';
                echo 
    '<option value="'.$c.'" '.$selected.'>'.$c.'</option>';
            }

        
    ?>
        </select>
        <input type="submit" name="submit" value="Submit" />
    </form>

  3. #3
    SitePoint Enthusiast adamschroeder's Avatar
    Join Date
    Mar 2011
    Location
    World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks centered effect it worked.
    So to see if theoretically I got it. This boolean is actually saying:

    If $c is within the posted array, then $selected equals selected. But if it's not within the array, then $selected equals nothing (' '). And then echo out the selected value.
    Is that correct? Am I understanding it correctly?

  4. #4
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, correct

  5. #5
    SitePoint Zealot FaridHadi's Avatar
    Join Date
    Nov 2008
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey adamschroeder,

    It's probably just a typo but in the code you posted you're trying to close the select element with <selected>. It should be </select>.

  6. #6
    SitePoint Enthusiast adamschroeder's Avatar
    Join Date
    Mar 2011
    Location
    World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Farid,
    luckily I used a different script so this typo was not an issue. But thanks anyway.


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
  •