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


No announcement yet.

Drop down list help

  • 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);
    $sql "SELECT value FROM table WHERE userID=$userID";
    $rs mysql_query($sql$id);
    $r mysql_fetch_assoc($rs);
    $value $r['value'];

    Then just echo out the value in your input tag

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

    <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>
    Last edited by bcarl314; Feb 11, 2004, 01: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


    • #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 (
      "<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