dropdownbox - how to set selected dropdown list in html -
this code dropdown list,plz guide me.
<div class = "form-inline"> <label class = "">situation</label> <select selected="" name="sit" id="situ" class='form-control'style="margin-left: 30px; width: 145px;"> <option value="" >select</option> <? if ($situation != '') { $revise = $situation; } foreach ($revise $row): ?> <option value="<?= $row['id']; ?>"><?= $row['situation']; ?></option> <? endforeach; ?> </select> </div>
for item want selected dropdown list, add attribute selected
.
eg.
<select> <option value="0">0</option> <option value="1" selected="selected">1</option> <option value="2">2</option> </select>
in case, 1 selected.
to value of dropdown after form submit, depends on method of form being post or get.
in example, i'm assuming it's post. <form method="post">
<div class = "form-inline"> <label class = "">situation</label> <select selected="" name="sit" id="situ" class='form-control'style="margin-left: 30px; width: 145px;"> <option value="" >select</option> <? if (isset($_post['sit'])) $selected = $_post['sit']; if ($situation != '') { $revise = $situation; } foreach ($revise $row): $selectedhtml = ''; if (isset($selected) && $row['id'] == $selected) $selectedhtml = ' selected="selected"'; ?> <option value="<?= $row['id']; ?>"<?= $selectedhtml; ?>><?= $row['situation']; ?></option> <? endforeach; ?> </select> </div>
you can try this.
if (isset($_post['sit'])) $selected = $_post['sit'];
what check if $_post['sit']
value found. if yes we'll make $selected
, else we'll leave it.
$selectedhtml = ''; if (isset($selected) && $row['id'] == $selected) $selectedhtml = ' selected="selected"';
and in foreach loop, first initialize $selectedhtml
empty string. not add <option>
unless got value changed.
we check if $selected
defined or not , if is, check if current value of $row['id']
within foreach loop equal submitted value $selected
. if so, set $selectedhtml
add selected="selected"
current <option>
.
Comments
Post a Comment