views:

32

answers:

3

Hi

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.

+2  A: 

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>
oezi
uhm, the question says html, not php etc. how do you know you can execute dynamic code?
seanizer
i hope so, because i don't know how to do this without dynamic code - and i showed php because its widely used (and doing the same in any other dynamic language would be very similar)
oezi
still, a phrase like "assuming you're using php" would have been appropriate.
seanizer
i'll note that for the next time ;)
oezi
+1  A: 

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>';

}
WEBProject
as above: the question says html, not php etc. how do you know you can execute dynamic code?
seanizer
A: 

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)

seanizer