Jump to content

How to retain values in simple combo upon onchange event


theITvideos

Recommended Posts

Hi there,

 

I am having a PHP form with simple combo box. Here is the code:

 

<form name="form1" method="get" action="">
  <select name="select" onChange="form1.submit()">
    <option value='value1'>My Value1</option>
    <option value='value2'>My Value2</option>
    <option value='value3'>My Value3</option>
  </select>
</form>

 

Now the form is successfully being submitted upon 'onChange' event. The only thing I am trying to do is to retain the value of the new option. As every time I select the value from the combo, it goes back and displays the 'value 1' even when I select value 3 or 2.

 

How do I make the combo box retain the value as 3 (when I select option 3) after the form is submitted.

 

Please reply. :)

 

Thank you!

 

 

Link to comment
Share on other sites

Add 'selected' to the element you want to be selected by default...

<option value='value1' selected>My Value1</option>

 

or w3c safe version... (need to check though)

<option value='value1' selected='selected'>My Value1</option>

 

Thanks for the reply. but I don't want 'value1' to be selected every time. As I may choose value 3 or value 2 from the combo box, hence it must display value 3 or value 2 as selected.

 

How can we do that. And just a quick reminder that I am submitting the form everytime we change the value in the combo box using an 'onchange' event and I just want the new changed (selected) value to retain in the combo.

 

Thanks!

Link to comment
Share on other sites

LOL, as you generate the form, you use the data from when it was submitted and decide in which option element to add the 'selected' attribute to.

 

I need to use the combo box value for some calculations. And I am submitting the form to the same page with the 'onchange' event.

 

I have no problem in getting the selected value from the combo box. Just need the combo box to retain the value in the combo box.

 

Because once the form comes back after the form.submit (I am submitting to the same page) the combo box value goes back to value 1

 

How do I get the combo box to retain the value I selected.

Link to comment
Share on other sites

You need to compare the value of what's in the $_POST array to the values, and if it matches, echo selected="selected". This could be much less coding and simpler if you build the select list from an array or DB query result, but this is the basic idea:

<select name="select" onChange="form1.submit()">
    <option value='value1' <?php echo (isset($_POST['select']) && $_POST['select'] == 'value1') ? ' selected="selected"' : ''; ?>>My Value1</option>
    <option value='value2' <?php echo (isset($_POST['select']) && $_POST['select'] == 'value2') ? ' selected="selected"' : ''; ?>>My Value2</option>
    <option value='value3' <?php echo (isset($_POST['select']) && $_POST['select'] == 'value3') ? ' selected="selected"' : ''; ?>>My Value3</option>
</select>

 

P.S. In case you aren't familiar with it, the syntax above makes use of the ternary operator.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.