开发者

How to automatically select the selected item in list menu after submitting the form?

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.com</option>
              <option value="paris">Paris.com</option>
              <option value="imo">IMO.com</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.com) -> form submit -> selected item will be "-------"

I want this to happen :

The user select the first item (Medmo.com) -> form submit -> selected item will be "Medmo.com"

How I can do that?

Thanks in Advance.


one possibility would be this:

<option value="medmo"<? if($mpage=='medmo') echo ' selected="selected"'; ?>>Medmo.com</option>
<option value="paris"<? if($mpage=='paris') echo ' selected="selected"'; ?>>Paris.com</option>
<option value="imo"<? if($mpage=='imo') echo ' selected="selected"'; ?>>IMO.com</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].'.com</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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜