z9z Posted October 4, 2010 Share Posted October 4, 2010 Hey guys i am new around here and i need some help with php. So basically i want to make a submit form and i found a problem for myself here's some images to understand what i am talking about. so here's what i've made at first. This is meant for new data. <p><label>Status :</label> <select name="status" > <option value=""></option> <option value="Ongoing">Ongoing</option> <option value="Completed">Completed</option> </select> basically what I want is when I edit the data, that selection returns the value that was stored in the database rather than just null value. See image below to understand what i am trying to do. i dont really get how to do it with select. Please help me and thanks before. Sorry if my question was already asked before. Tried to search but didnt really found what i am looking for. Quote Link to comment https://forums.phpfreaks.com/topic/215145-need-help-with-form-select-drop-down-box/ Share on other sites More sharing options...
Psycho Posted October 4, 2010 Share Posted October 4, 2010 You will need server-side code to build the select list instead of hard-coding it. <?php $statusOptionsList = array('', 'Ongoing', 'Completed'); $statusOptionsHTML =''; foreach($statusOptionsList as $option) { $selected = ($userValueFromDB==$option) ? ' selected="selected"' : ''; $statusOptionsHTML .= "<option value=\"{$option}\"{$selected}>{$option}</option>"; } ?> <select name="status"> <?php echo $statusOptionsHTML; ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/215145-need-help-with-form-select-drop-down-box/#findComment-1119004 Share on other sites More sharing options...
z9z Posted October 4, 2010 Author Share Posted October 4, 2010 ah that's it "selected" thats new for me, didn't know that attributes exist I've tried it and it works well thanks. $selected = ($userValueFromDB==$option) ? ' selected="selected"' : ''; is that boolean expression the same right this ? if ($userValueFromDB==$option) $selected = "selected"; else $selected = ""; sorry again for stupid question. I rarely see boolean expression used. Anyway thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/215145-need-help-with-form-select-drop-down-box/#findComment-1119025 Share on other sites More sharing options...
Psycho Posted October 4, 2010 Share Posted October 4, 2010 You are correct in how you think that statement is interpreted, but it is the comparison that is boolean (true or false) and it is the same comparison either way. The term for the solution I provided is called the ternary operator. Many people refer to it as a shorthand if/esle statement, but that is an understatement in my opinion. Quote Link to comment https://forums.phpfreaks.com/topic/215145-need-help-with-form-select-drop-down-box/#findComment-1119029 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.