| SELECTED in drop down
|
basketmen

msg:4380380 | 8:06 pm on Oct 27, 2011 (gmt 0) | Hi guys, i have a drop down code below <select class="input" name="mod[cpu]"> <option value=""> </option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> |
| the output are like this | $html .= ($content[cpu] ? '<div class="' . $this->switch_bg() . '"><strong>CPU:</strong> ' . @htmlspecialchars($content[cpu]) . '</div>' : ''); |
| i want after the drop down choosed by user, it will add SELECTED next time they opening the drop down again i am still dont know how to put the SELECTED there, please help what is the code to add SELECTED, i only have variable mod[cpu] and $content[cpu] i already tried in bold below, but still not working <select class="input" name="mod[cpu]" selected="$content[cpu]"> <option value=""> </option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> |
| please share your knowledge
|
penders

msg:4380430 | 10:03 pm on Oct 27, 2011 (gmt 0) | The SELECTED attribute needs to go on whatever OPTION element was previously selected, it doesn't go directly on the SELECT element.
|
rocknbil

msg:4380787 | 5:50 pm on Oct 28, 2011 (gmt 0) | To get one thing out of the way: are you using an XHTML doctype or HTML/HTML5? For HTML, it's <option value="some-value" selected> for XHTML, it's <option value="some-value" selected="selected"> | i want after the drop down choosed by user, it will add SELECTED next time they opening the drop down again |
| You **should** have these select lists generated by a function, but this simple example assumes this form is using POST as a method. I don't use form arrays often, doesn't look like you need them, so to avoid "incorrect code" I'm using a static name. Change the names as needed . . . $using_xhtml = 0; // because most of us don't need to . . $selected_syntax=($using_xhtml==1)?' selected="selected"':' selected'; } $vals = array ('1','2','3','4'); $this_select = ' <select class="input" name="cpu"> <option value=""> </option> '; foreach ($vals as $val) { $this_select .= '<option value="' . $val . '"'; if (isset($_POST['cpu']) and ($_POST['cpu']==$val)) { $this_select .= $selected_syntax; } $this_select .= '>' . $val . '</option>'; } $this_select .= '</select>'; echo $this_select; I'd also advise against using selectors that may conflict with the dom, such as input, search, select, display, etc . . . it gets very confusing .input input {}
|
|
|