Jump to content

make <select> tag option remain selected


vinpkl

Recommended Posts

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

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>	

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.

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>	

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.

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 :P

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.

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.