admin管理员组文章数量:1406177
I have this code :
<form id="form2" name="form2" method="post" action="">
<table dir="ltr" width="554" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="269" class="da"><div align="center"><span id="spryselect1">
<select onchange="form2.submit()" name="mpage" id="mpage">
<option selected="selected" value="no">-----------</option>
<option value="medmo">Medmo</option>
<option value="paris">Paris</option>
<option value="imo">IMO</option>
</select>
</span></div></td>
<td width="214" class="t_b">Select Website</td>
</tr>
</table>
</form>
When the user select a value, the form will automatically submit, I want the item that the user has selected to be selected after submitting the form.
Because I am facing this proplem:
The user select the first item (Medmo) -> form submit -> selected item will be "-------"
I want this to happen :
The user select the first item (Medmo) -> form submit -> selected item will be "Medmo"
How I can do that?
Thanks in Advance.
I have this code :
<form id="form2" name="form2" method="post" action="">
<table dir="ltr" width="554" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="269" class="da"><div align="center"><span id="spryselect1">
<select onchange="form2.submit()" name="mpage" id="mpage">
<option selected="selected" value="no">-----------</option>
<option value="medmo">Medmo.</option>
<option value="paris">Paris.</option>
<option value="imo">IMO.</option>
</select>
</span></div></td>
<td width="214" class="t_b">Select Website</td>
</tr>
</table>
</form>
When the user select a value, the form will automatically submit, I want the item that the user has selected to be selected after submitting the form.
Because I am facing this proplem:
The user select the first item (Medmo.) -> form submit -> selected item will be "-------"
I want this to happen :
The user select the first item (Medmo.) -> form submit -> selected item will be "Medmo."
How I can do that?
Thanks in Advance.
Share Improve this question edited Jun 11, 2010 at 14:40 Sean Patrick Floyd 300k72 gold badges476 silver badges595 bronze badges asked Jun 11, 2010 at 14:02 SalehSaleh 2,71912 gold badges46 silver badges75 bronze badges 2- plain html, or are you using PHP, ASP, rails, java etc.? because everybody else seems to think php is the context – Sean Patrick Floyd Commented Jun 11, 2010 at 14:17
- ok, edited tags (I'm outta here, PHP is not my cup of tea :-)) – Sean Patrick Floyd Commented Jun 11, 2010 at 14:41
3 Answers
Reset to default 3one possibility would be this:
<option value="medmo"<? if($mpage=='medmo') echo ' selected="selected"'; ?>>Medmo.</option>
<option value="paris"<? if($mpage=='paris') echo ' selected="selected"'; ?>>Paris.</option>
<option value="imo"<? if($mpage=='imo') echo ' selected="selected"'; ?>>IMO.</option>
Another way, more elegant:
use ids (numbers) and then just make a loop, to check if the post number matches the current number, you can do it with an array:
// 0 ,1, 2
$ids = ('Medmo','Paris','Imo');
$selected = $_POST['mpage'];
for($i=0;$i<count($ids);$i++)
{
if($ids[$i] == $selected)
{
$selected = 'selected="selected"';
}
print '<option value="'.$i.'" '.$selected.'>'.$ids[$i].'.</option>';
}
you can't do that in html alone (nor in javascript).
when a form is submitted, a new page is loaded, and you don't have any influence on what happens there.
so you either need some server side framework (php, rails, java, whatever) or you could work with cookies and javascript (store the selected value in a cookie and initialize the new page from this cookie value)
版权声明:本文标题:php - How to automatically select the selected item in list menu after submitting the form? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744961120a2634660.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论