I have a select box where the user chooses a sex (gender) from a drop down, and their choice is specified with “selected” in the select tag. But the box always ends up ‘Choose’. What is wrong in the following syntax?
echo"<select name='sex'>
<option value='N' '". ($info['sex'] == "N" ? 'selected=selected':'') ."'>Choose</option>
<option value='M' '". ($info['sex'] == "M" ? 'selected=selected':'') ."'>Male</option>
<option value='F' '". ($info['sex'] == "F" ? 'selected=selected':'') ."'>Female</option>
</select>";
The $info['sex'] is from a consult SQL that return always N, M or F.
Your
selected=selectedis being quoted and output likewhen you run your code, use this
notice the escaped quotes at
'selected=\'selected\''and the lack of single-quotes at". ($info['sex']and.">demo: http://codepad.org/AX95BzTR
here’s a fiddle showing your problematic output: http://jsfiddle.net/JKirchartz/KB4rv