Jump to content

Select default option in dynamic drop down menu


mfaulkner

Recommended Posts

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:

[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>[/code]


Edit: the field [b]status[/b] is --  ENUM('Available','Booked')

[attachment deleted by admin]
first of all, you pre-select an option from a dropdown menu with [code]selected="selected"[/code]

you just use[code]<option value="1" name="something" SELECTED>[/code]

and second, THIS is how you write the short if statement...
[code]<option value="2" <?php (($row['status'] == "2") ? " SELECTED : ""); ?>>Booked</option>[/code]
Here's the code relating to mfa..'s edit:
[code]
<option value="2" <?=(($row['status'] == "Booked")?(' selected="selected"'):(""))?>>Booked</option>
<option value="2" <?=(($row['status'] == "Available")?(' selected="selected"'):(""))?>>Available</option>
[/code]
well, shogun's code is more related to YOUR usage. mine is just for an example. however, beyond that, they may still look a little different. this is just a matter of personal preference. i think shogun's is easier to read and understand. i've never seen the short if statement being used that way. nice shogun.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.