matthew9090 Posted April 2, 2011 Share Posted April 2, 2011 i have a basic html form with a drop down selection box with 2 options ('old' and 'new'). when you click one it swaps them round to you can click the other because it is an onchange event. <form action="<?php $_SERVER['self']; ?>" method="get"> <select name="Time" onchange="this.form.submit();"> <option value="New">New</option> <option value="Old">Old</option> </select></form> Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 2, 2011 Share Posted April 2, 2011 Is that a question? Quote Link to comment Share on other sites More sharing options...
matthew9090 Posted April 3, 2011 Author Share Posted April 3, 2011 sorry i meant i have a basic html form with a drop down selection box with 2 options ('old' and 'new'). but i don't know how to swap the drop down options around once you've clicked the other because there is only 2 options you can't click the other one. Quote Link to comment Share on other sites More sharing options...
matthew9090 Posted April 3, 2011 Author Share Posted April 3, 2011 i'm thinking something like this? <?php if (isset($_GET['Time'])) { ?> <script type="text/javascript"> //javascript code here </script> <?php } ?> <form action="<?php $_SERVER['self']; ?>" method="get"> <select name="Time" onchange="this.form.submit();"> <option id="1" value="New">New</option> <option id="2" value="Old">Old</option> </select></form> but i don't know how to use javascript to swap them round. any ideas? Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 4, 2011 Share Posted April 4, 2011 One of the tags needs to include the selected attribute. I'm not sure how you're planning to use this but it is not a question of javascript, since you're submitting the form anytime this is changed. Instead, it's a question of examining the $_GET[] for the current value and setting the selected to be that value in your php script. This is because you specified the method of the form to be get, but if it was a "post" method as most forms are, you'd look at $_POST. $timeoptions = array(1 => 'New', 2 => 'Old'); if (isset($_GET['Time'])) { $selectoption = ($_GET['Time'] == 'Old') ? 'Old' : 'New'; } ?> </pre> <form action="<?php%20%24_SERVER%5B'self'%5D;%20?>" method="get"> foreach ($timeoptions as $id => $value) { $selected = ($selectoption == $value) ? ' selected' : ''; echo "$value"; } ?> </form Quote Link to comment Share on other sites More sharing options...
matthew9090 Posted April 4, 2011 Author Share Posted April 4, 2011 thanks for all your help but i've found a simpler solution, just to add 1 more blank option at the top. Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 4, 2011 Share Posted April 4, 2011 thanks for all your help but i've found a simpler solution, just to add 1 more blank option at the top. Which does what exactly? Quote Link to comment Share on other sites More sharing options...
matthew9090 Posted April 4, 2011 Author Share Posted April 4, 2011 the problem was that because it is an onchange function, if it has only 2 options then it would start on the first one so you can only change to the second option so by adding a third one at the top you can click both. 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.