theITvideos Posted October 25, 2010 Share Posted October 25, 2010 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! Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 25, 2010 Share Posted October 25, 2010 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> Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 25, 2010 Share Posted October 25, 2010 Yeah, use the second version... http://www.w3schools.com/TAGS/att_option_selected.asp Quote Link to comment Share on other sites More sharing options...
theITvideos Posted October 25, 2010 Author Share Posted October 25, 2010 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! Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 25, 2010 Share Posted October 25, 2010 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. Quote Link to comment Share on other sites More sharing options...
theITvideos Posted October 25, 2010 Author Share Posted October 25, 2010 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. Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 25, 2010 Share Posted October 25, 2010 LOL, re-read what I wrote... It's all there... Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 25, 2010 Share Posted October 25, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.