Web Analytics Made Easy -
StatCounter Drop down list help - CodingForum

Announcement

Collapse
No announcement yet.

Drop down list help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Drop down list help

    Hi, I am trying to build a page which allows registered users to edit their details. The first field is "Title". I want this to be a drop down box as there are limited values. Now I have no problem in creating a dropdown box but I want it to default to the value they have already in the database.

    Could anyone give me some pointers as to how is best to do this? Cheers, Andy.

  • #2
    I'm assuming you know how to connect to the database and get the default value...

    something like...
    PHP Code:
    $id mysql_connect(hostusernamepassword);
    mysql_select_db(dbname$id);
    $sql "SELECT value FROM table WHERE userID=$userID";
    $rs mysql_query($sql$id);
    $r mysql_fetch_assoc($rs);
    $value $r['value'];
    mysql_free_result($rs); 

    Then just echo out the value in your input tag

    Code:
    <input type="text" name="fieldName" value="<?php echo $value;?>" />
    <edit>
    opps, sorry you wanted for a drop down...

    Code:
    <select name="myselect">
        <option value="value1" <?php if($value=="value1"){echo selected=\"selected\";}?>>Option 1</option>
        <option value="value2" <?php if($value=="value2"){echo selected=\"selected\";}?>>Option 2</option>
    </select>
    </edit>
    Last edited by bcarl314; Feb 11, 2004, 12:51 PM.
    Create accessible online surveys -::- Koobten.com - compare netbook prices and reviews -::- Affordable, reliable hosting for less than $20 per year
    Zend Certified Engineer

    Comment


    • #3
      Thats a great help, thanks. Did it slightly differently but same idea. Im having a problem now though, were i can populate the dropdown but it always misses the first entry from the db table. I assume its something to do with the way the arrays are indexed. This is what i have:

      PHP Code:
       print "<select>";   
       while (
      $row=mysql_fetch_array($result)){
          
      $title=$row["title"];
          print 
      "<option value='$title'>$title</option>";         
       } 
      if this is more of a mysql question then i apologise but if anyone can help it would be much appreciated

      Comment

      Working...
      X