SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Nov 2000
    Posts
    740
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Setting List control to value

    I have an edit.php page where I want to populate the values of some fields. This is fine for the text fields but I'm not sure how to achieve this for the List controls.

    The HTML for my List control is below. How can I set the value to display what is stored in the MySQL backend?

    HTML Code:
    <td valign="top">CategoryID</td>
    	  <td><select name="CategoryID" id="CategoryID">
    		  <option>Business</option>
    		  <option>Financial</option>
    		  <option>Health</option>
    		  <option>Home</option>
    		  <option>Legal</option>
    		  <option>Shopping</option>
    		  <option>Travel</option>
    		</select></td>

  2. #2
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $sql_1 "my query that gets data for whole <select> list here"
      
    $qry_1 mysql_query($sql_1) or die(mysql_error());
      
      
    $my_names = array();
      while(
    $data_1 mysql_fetch_assoc($qry_1))
      {
      
    $my_names[$data_1['ID']] = $data_1['Name'];
      }
      
      
    $default_from_other_query "Roger";
      
      
    $my_list "<select name='name'>";
      foreach(
    $my_names as $id=>$name)
      {
      
    $selected "";
      if(
    $default_from_other_query == $name)
      {
      
    $selected " selected='selected'";
      }
     
    $my_list .= "<option value='$id'{$selected}>{$name}</option>";
     }
     
    $my_list .= "</select>"

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Use <option selected value="value">description</option> for the value you want to display.

    Look here: http://www.htmlhelp.com/reference/ht...ms/option.html

    Remember you need to use the 'value' attribute if you want to use the value selected after form submit.

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Just a footnote.
    If you don't specify a value, the select menu passes the displayed value as the post.
    So:
    <option selected value="mydescription">description</option>
    would send mydescription as the value
    <option>description</option>
    would send description as the value.
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,496
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Didn't know that spikeZ, thx.


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
  •