mfaulkner Posted January 29, 2007 Share Posted January 29, 2007 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]<?phprequire_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] Link to comment https://forums.phpfreaks.com/topic/36219-select-default-option-in-dynamic-drop-down-menu/ Share on other sites More sharing options...
boo_lolly Posted January 29, 2007 Share Posted January 29, 2007 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] Link to comment https://forums.phpfreaks.com/topic/36219-select-default-option-in-dynamic-drop-down-menu/#findComment-172070 Share on other sites More sharing options...
ShogunWarrior Posted January 29, 2007 Share Posted January 29, 2007 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] Link to comment https://forums.phpfreaks.com/topic/36219-select-default-option-in-dynamic-drop-down-menu/#findComment-172078 Share on other sites More sharing options...
mfaulkner Posted January 29, 2007 Author Share Posted January 29, 2007 Ok thanks for the replies guys but I'm confused on which code to use as they differ ??? Link to comment https://forums.phpfreaks.com/topic/36219-select-default-option-in-dynamic-drop-down-menu/#findComment-172090 Share on other sites More sharing options...
boo_lolly Posted January 29, 2007 Share Posted January 29, 2007 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. Link to comment https://forums.phpfreaks.com/topic/36219-select-default-option-in-dynamic-drop-down-menu/#findComment-172149 Share on other sites More sharing options...
ShogunWarrior Posted January 29, 2007 Share Posted January 29, 2007 mfaulkner, it depends on your actual data. My code should work on the basis that $row['status'] will either be "Available" or "Booked", whereas you had "1"/"2". Link to comment https://forums.phpfreaks.com/topic/36219-select-default-option-in-dynamic-drop-down-menu/#findComment-172152 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.