SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    london
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    re-populating an option/dropdown form control

    hi there, this is probably a really simple question but I have a form with amongst other fields a drop down list for Jan,Feb,Mar ....etc and it is submitted to a mysql database.

    I am creating an edit function for all records in the database which when clicked on will load each record back into a form similar to the one it was originally submitted on

    my question is, How do i get the value entered from the dropdown ie "Apr" to be the selected/default option when the form is reloaded for editing. I know how to repopulate a textarea but have absolutely no clue on how id do this, the same goes for option boxee aswell, but i presume if someone gives me a pointer on one, ill work the other one out quite easily

    any help on this would be brilliant

  2. #2
    SitePoint Addict
    Join Date
    Jan 2004
    Location
    Colorado Springs
    Posts
    308
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <option selected value="<?php echo $month; ?>"><?php echo $month; ?></option>

    Just have the date you want select with <option selected

  3. #3
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a small function I use...
    PHP Code:
    function monthoptions($current)
    {
        
    $months = array("","January""February""March""April""May""June""July"
                                
    "August""September""October""November""December");
        for( 
    $i 1$i 13$i++)
        {
            echo 
    "<option value=\"$i\"";
            if( 
    $i == $current ) echo " SELECTED";
            echo 
    ">".$months[$i]."</option>\n";
        }

    A typical call would be this...
    PHP Code:
        echo "<select name=\"start_month\">";
        
    monthoptions($smonth);
        echo 
    "</select>"
    If $smonth contains November, it will be selected.
    Lats...

  4. #4
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    london
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nekeno12, thats great, thankyou, but there is a slight annoyance with this in that because I dont know what month will be pulled from the database, I have to still create an option for each month, see code below

    HTML Code:
    <body>
    
    <?php
    
    $month = "Feb"
    
    <!-- $month will actually be called from the database -->
    
    
    ?>
    <form><select name="startmonth">
    <option selected value="<?php echo $month; ?>"><?php echo $month; ?></option>
    
                          <option>Jan</option>
                          <option>Feb</option>
                          <option>Mar</option>
                          <option>Apr</option>
                          <option>May</option>
                          <option>Jun</option>
                          <option>Jul</option>
                          <option>Aug</option>
                          <option>Sep</option>
                          <option>Oct</option>
                          <option>Nov</option>
                          <option>Dec</option>
                        </select></form>
    
    
    
    
    
    
    </body>
    when this is loaded it does indeed create the default date of Feb (as per $month) but when you click on the dropdown it appears like this

    Feb
    Jan
    Feb
    Mar
    Apr
    May
    ..
    ..
    etc

    is there any way to get rid of this duplicate entry

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    london
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Lats ill give that a go and post back, could you just explain what $current is in your example ??

    cheers

  6. #6
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $current is just the receiving variable name, in this case is just catches $smonth.
    Lats...

  7. #7
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I just make sure the values in the <options> of the select box will match the value of the month coming from the DB.
    Then I do this (where $month contains the relevant month that must match the value of the option)
    HTML Code:
     <option value="Jan" <? if($month=="Jan") echo "selected";?> >January</option>
     <option value="Feb" <? if($month=="Feb") echo "selected";?> >February</option>
     <option value="Mar" <? if($month=="Mar") echo "selected";?> >March</option>
     ...
    It's not overly elegant, but its simple and works. You then retain the correct month ordering in the select box, and the correct month will be selected.

    Btw, you can echo checked in a radio or checkbox if you need to automatically select it for the same reason.

  8. #8
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    london
    Posts
    160
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks that works perfectly, i have used

    PHP Code:
    <option value="Jan" <? if($month=="Jan") echo "selected";?>
    you mentioned briefly how to get it working with radio boxes, but i cant seem to get it running, my radio buttons currently look like this

    HTML Code:
    <input type=radio value=501 name=branch>501 <br />
    <input type=radio value=502 name=branch>502
    How can i use the same system to re-populate the option controls ??

  9. #9
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
     <input type=radio value=502 name=branch <? if($branch==502) echo "checked";?> >502
    Just like that. It is exactly the same concept as the select box, but you need to echo the word checked instead of selected to check a radio button.


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
  •