www.webdeveloper.com
Results 1 to 9 of 9

Thread: Select default option in dynamic drop down menu

  1. #1
    Join Date
    Jan 2007
    Posts
    30

    Question Select default option in dynamic drop down menu

    Hi all, I'm having abit of trouble working out how to automatically default to the previous option entered into MySQL via an HTML form. Its updating the database, not inserting new vales, so its vital that when loading the update page the stored values are selected by default. Heres the form code for what I've done so far:

    HTML Code:
    <td class="<?php echo $jan_1; ?>">1. <select name="jan_1" size="1">
    <option value="Available">Available</option>
    <option value="Booked">Booked</option>
    </select>
    </td>
    Any help would be really grateful. Thanks in advance,

    Mark.

  2. #2
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    You don't say what the field is called, so I'll give it a name: $row['jan_1'].
    PHP Code:
    <td class="<?php echo $jan_1?>">1. <select name="jan_1" size="1">
    <option value="Available" <?php echo ($row['jan_1']=="Available" 'selected="selected"' ''); ?>>Available</option>
    <option value="Booked" <?php echo ($row['jan_1']=="Booked" 'selected="selected"' ''); ?>>Booked</option>
    </select>
    </td>

  3. #3
    Join Date
    Jan 2007
    Posts
    30
    NightShift58, thanks for your reply. I tried the code you suggested but its still not displaying the current value as the default option. I'll provide more information in any case, maybe the problem will become clearer then.

    The database is structured as follows:

    FIELD TYPE
    jan_1 enum('Available', 'Booked')
    jan_2 enum('Available', 'Booked')
    etc...

    Also, I'm not really too sure what your suggested code actually does.

    Thanks in advance guys,

    Mark.

  4. #4
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Sorry... enum is an integer value and not a string. Try this:
    PHP Code:
    <td class="<?php echo $jan_1?>">1. <select name="jan_1" size="1"> 
    <option value="1" <?php echo ($row['jan_1'] == "1" 'selected="selected"' ''); ?>>Available</option> 
    <option value="2" <?php echo ($row['jan_1'] == "2" 'selected="selected"' ''); ?>>Booked</option> 
    </select> 
    </td>
    This is supposed to highlight the item previously chosen by the user - actually highlight the option store in the tabel.

  5. #5
    Join Date
    Jan 2007
    Posts
    30
    Thanks again for your reply, but still no luck I'm afraid.

    PHP Code:
    <?php
    $retrievejan 
    mysql_query("SELECT * FROM january")
        or die(
    mysql_error());
    while (
    $rows mysql_fetch_array($retrievejan)) {
        
    extract($rows);
    }
    ?>
    Thats the code I'm using to retrieve all the data from the database and I've tried the modification you suggested. My form is updating the database and the data is also being displayed correctly. Really stumped now :s

  6. #6
    Join Date
    Dec 2006
    Location
    Escaz˙ (Costa Rica) and Mallorca (Spain)
    Posts
    3,234
    Quote Originally Posted by NightShift58
    You don't say what the field is called, so I'll give it a name: $row['jan_1'].
    PHP Code:
    <td class="<?php echo $jan_1?>">1. <select name="jan_1" size="1">
    <option value="Available" <?php echo ($row['jan_1']=="Available" 'selected="selected"' ''); ?>>Available</option>
    <option value="Booked" <?php echo ($row['jan_1']=="Booked" 'selected="selected"' ''); ?>>Booked</option>
    </select>
    </td>
    I told you I didn't know the name of your field. I used that name for example purposes. You'll have to rename it to match your field name. I doubt I guessed it right and if I did, I'll start palying the lottery...

  7. #7
    Join Date
    Jan 2007
    Posts
    30
    lol, but you guessed right, it was called 'jan_1'. thanks for your help anyhow, I will investigate it more tomorrow.

  8. #8
    Join Date
    Jan 2007
    Posts
    30

    Question

    Still no luck, this is the code I'm using and I've attached a screenshot of my database structure. I've tried lots of different combinations but cannot see why it isnt working.

    PHP Code:
    <?php
    require_once('../includes/dbconnect.php');

    $result mysql_query("SELECT * FROM january")
        or die(
    mysql_error());
        
    $row mysql_fetch_array($result)
        or die(
    mysql_error());    
    ?>

    <form>

    <select name="jan_1" size="1">
    <option value="1" <?php echo ($row['status'] == "1" 'selected="selected"' ''); ?>>Available</option>
    <option value="2" <?php echo ($row['status'] == "2" 'selected="selected"' ''); ?>>Booked</option>
    </select>

    </form>
    Attached Images Attached Images

  9. #9
    Join Date
    Jan 2007
    Posts
    30

    Thumbs up Problem Solved

    Problem Solved:

    Heres the code which eventually answered my question:

    PHP Code:
    <select name="jan_status_1" size="1">
      <option value="Available"<?php echo ($row_1['jan_status'] == 'Available') ? ' selected' '' ?>>Available</option>
      <option value="Booked"<?php echo ($row_1['jan_status'] == 'Booked') ? ' selected' '' ?>>Booked</option>
    </select>
    I understand how the above shortened IF statements work now, thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles