vinpkl Posted May 26, 2009 Share Posted May 26, 2009 hi all i m using the form in which i m using <select> tag to display options <form action="" method="post" name="form1" id="form1" style="margin-bottom:0px;"> Sort by <select name="choice" id="choice"> <option>select choice</option> <option value="high">Price high to low</option> <option value="low">Price low to high</option> </select> <input name="submit" id="submit" type="submit" value="click" /> </form> after choosing the option and clicking the submit button i m able to display the results fine. but the problem is that on clicking the submit button the "selected option" doesnt remain "selected". how can i make it remain selected untill the user himself changes the option. vineet Link to comment https://forums.phpfreaks.com/topic/159671-make-tag-option-remain-selected/ Share on other sites More sharing options...
Jibberish Posted May 26, 2009 Share Posted May 26, 2009 Try chaging it to this. <form action="" method="post" name="form1" id="form1" style="margin-bottom:0px;"> Sort by <select name="choice" id="choice"> <option>select choice</option> <option <?php if($_POST['choice'] == 'high') echo 'selected'; ?> value="high">Price high to low</option> <option <?php if($_POST['choice'] == 'low') echo 'selected'; ?> value="low">Price low to high</option> </select> <input name="submit" id="submit" type="submit" value="click" /> </form> Link to comment https://forums.phpfreaks.com/topic/159671-make-tag-option-remain-selected/#findComment-842115 Share on other sites More sharing options...
GingerRobot Posted May 26, 2009 Share Posted May 26, 2009 Edit: Beaten to it, but you should be checking to see if the values have actually been posted, otherwise you might be an undefined index notice. To have an option selected, you need to set the selected tag to "selected". So, you want something like this: <form action="" method="post" name="form1" id="form1" style="margin-bottom:0px;"> Sort by <select name="choice" id="choice"> <option>select choice</option> <option value="high" <?php echo (if isset($_POST['choice']) && $_POST['choice']=='high') ? 'selected="selected"' : '';?>>Price high to low</option> <option value="low" <?php echo (if isset($_POST['choice']) && $_POST['choice']=='low') ? 'selected="selected"' : '';?>>Price low to high</option> </select> <input name="submit" id="submit" type="submit" value="click" /> </form> If you have lots of options in your select box, it'd be easier to set up an array to loop through. Link to comment https://forums.phpfreaks.com/topic/159671-make-tag-option-remain-selected/#findComment-842116 Share on other sites More sharing options...
vinpkl Posted May 26, 2009 Author Share Posted May 26, 2009 hi Jibberish thanks for the reply. it works perfect vineet Try chaging it to this. <form action="" method="post" name="form1" id="form1" style="margin-bottom:0px;"> Sort by <select name="choice" id="choice"> <option>select choice</option> <option <?php if($_POST['choice'] == 'high') echo 'selected'; ?> value="high">Price high to low</option> <option <?php if($_POST['choice'] == 'low') echo 'selected'; ?> value="low">Price low to high</option> </select> <input name="submit" id="submit" type="submit" value="click" /> </form> Link to comment https://forums.phpfreaks.com/topic/159671-make-tag-option-remain-selected/#findComment-842120 Share on other sites More sharing options...
Jibberish Posted May 26, 2009 Share Posted May 26, 2009 To have an option selected, you need to set the selected tag to "selected". So, you want something like this: Is that just a standards thing? As it does select it with just putting in "selected" as far as I know. (Sorry if its abit off topic, would just like to know though). And your welcome, but like GingerRobot said, you should also check to make sure its in the array just incase. Link to comment https://forums.phpfreaks.com/topic/159671-make-tag-option-remain-selected/#findComment-842121 Share on other sites More sharing options...
GingerRobot Posted May 26, 2009 Share Posted May 26, 2009 Is that just a standards thing? As it does select it with just putting in "selected" as far as I know. (Sorry if its abit off topic, would just like to know though). Yeah, it's standards-related. Attributes without values (i've a feeling there's a proper name for this, but it escapes me this morning) are valid HTML, but not valid XHTML. Personally i prefer to give the value in any case, as it looks better to me. Course, if you get 345353626426 hits a day, saving the extra few characters from your file and thus having a smaller file to serve might be more important Link to comment https://forums.phpfreaks.com/topic/159671-make-tag-option-remain-selected/#findComment-842122 Share on other sites More sharing options...
vinpkl Posted May 26, 2009 Author Share Posted May 26, 2009 hi gingerrobot i tried your code but got this error Parse error: syntax error, unexpected T_IF in E:\xampp\htdocs\gads\products.php on line 102 this i have on 102 <option value="high" <?php echo (if isset($_POST['choice']) && $_POST['choice']=='high') ? 'selected="selected"' : '';?>>Price high to low</option> vineet Edit: Beaten to it, but you should be checking to see if the values have actually been posted, otherwise you might be an undefined index notice. To have an option selected, you need to set the selected tag to "selected". So, you want something like this: <form action="" method="post" name="form1" id="form1" style="margin-bottom:0px;"> Sort by <select name="choice" id="choice"> <option>select choice</option> <option value="high" <?php echo (if isset($_POST['choice']) && $_POST['choice']=='high') ? 'selected="selected"' : '';?>>Price high to low</option> <option value="low" <?php echo (if isset($_POST['choice']) && $_POST['choice']=='low') ? 'selected="selected"' : '';?>>Price low to high</option> </select> <input name="submit" id="submit" type="submit" value="click" /> </form> If you have lots of options in your select box, it'd be easier to set up an array to loop through. Link to comment https://forums.phpfreaks.com/topic/159671-make-tag-option-remain-selected/#findComment-842124 Share on other sites More sharing options...
GingerRobot Posted May 26, 2009 Share Posted May 26, 2009 Heh, it's too early. There shouldn't be an if there at all: <option value="high" <?php echo (isset($_POST['choice']) && $_POST['choice']=='high') ? 'selected="selected"' : '';?>>Price high to low</option> <option value="low" <?php echo (isset($_POST['choice']) && $_POST['choice']=='low') ? 'selected="selected"' : '';?>>Price low to high</option> Link to comment https://forums.phpfreaks.com/topic/159671-make-tag-option-remain-selected/#findComment-842127 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.