Page 1 of 1

How to ensure list box remains selected when highlighted?

Posted: Mon Sep 29, 2008 11:15 pm
by swetha

Code: Select all

 
<input type="radio" name="convtext"  value="first" 
<?php if((isset($_POST['convtext'])) && ($_POST['convtext']=="first")) echo "checked"  ?>/>Convert first<br/>
<input type="radio" name="convtext"  value="second" 
<?php  if((isset($_POST['convtext'])) && ($_POST['convtext']=="second")) echo "checked"  ?>/>Convert second<br/>
 
The above code worked when i had written for a radio box,but when i wrote a similar code for list box,the code didnt work,the value "selected" gets inserted into the list box.Also the first value,in this case "8" remains selected always.

Code: Select all

 
<select name="listvalue" size="1">
<option value="eight"><?php if ((isset($_POST['listvalue'])) && ($_POST['listvalue']=='eight')) echo "selected" ?>8</option>
<option value="nine"><?php if ((isset($_POST['listvalue'])) && ($_POST['listvalue']=='nine')) echo "selected" ?>9</option>
</select>
 

Re: How to ensure list box remains selected when highlighted?

Posted: Mon Sep 29, 2008 11:22 pm
by Stryks
I think the format for marking an option as selected is ...

Code: Select all

<option value="eight" selected="selected">descriptive text</option>
If you adapt your code to output that, you should be set.

Cheers

Re: How to ensure list box remains selected when highlighted?

Posted: Tue Sep 30, 2008 12:18 am
by swetha
This doesnt work.i need to highlight the value in the list box only when it is selected.
The solution you have given me only works by default.

Re: How to ensure list box remains selected when highlighted?

Posted: Tue Sep 30, 2008 2:01 am
by papa
Submit the form and save values in hidden fields.

<select ... onchange='this.form.submit()>

Re: How to ensure list box remains selected when highlighted?

Posted: Tue Sep 30, 2008 2:03 am
by Stryks
I assume that you are referring to 'selected' as having an option automatically highlighted when you re-display a form that has been set previously.

In this case, my method DOES work. But you have to figure out which option is supposed to be selected and use PHP to set selected="selected" for the element you want to be selected. I'd post the code, but then I don't see how that's going to help you understand.

So look at the following.

Code: Select all

<select name="test1">
  <option value="one">1</option>
  <option value="two">2</option>
  <option value="three" selected="selected">3</option>
</select>
In that example, the third value is pre-selected. Yes it's hard coded, but then, we're here about applying PHP to it, yes?

So, given your if() statement is valid, how could you incorporate it into the example HTML in order to add selected="selected" to the one item that you want auto-selected. And here's a hint .. it's so incredibly close to your working snippet, it'll blow your mind.

Re: How to ensure list box remains selected when highlighted?

Posted: Tue Sep 30, 2008 4:07 am
by swetha
ive tried these 2 options:

Code: Select all

 
<option value="8"><?php if ((isset($_POST['listvalue'])) && ($_POST['listvalue']=='8')) 
 echo "selected='selected'"?> 8</option>
 

Code: Select all

 
<option value="8"><?php if ((isset($_POST['listvalue'])) && ($_POST['listvalue']=='8')) 
 selected="selected"?> 8</option>
 


both the above methods didnt work.

Re: How to ensure list box remains selected when highlighted?

Posted: Tue Sep 30, 2008 4:32 am
by papa
Like Stryks is saying. First of all your html is wrong...


<option value="" selected>yada yada</option>

not

<option value="">selected</option>

Re: How to ensure list box remains selected when highlighted?

Posted: Tue Sep 30, 2008 4:49 am
by Stryks
Well ...

Code: Select all

<option value="" selected>
... will work for most browsers, especially current generation, the recommended usage for maximum compatability across browsers is still ...

Code: Select all

<option value="" selected="selected">
But papa is dead right. To understand what your markup is doing, open the page, view it in the browser, and then right-click and 'view source' or 'view page source' depending on your browser. That way you can see the generated HTML after the PHP has done it's work.

If you look at it with either of those examples, you'll see that you're adding selected between the open and close tags, instead of on the inside of the opening option tag.

Again, look at papa's example. One is in the tag, one is between the open and close tags, where it will just be added to the option text.

One more testing iteration should do it ... you're really very close.

Re: How to ensure list box remains selected when highlighted?

Posted: Tue Sep 30, 2008 10:52 pm
by swetha

Code: Select all

 
<select name="listvalue" size="1">
<option value="eight"<?php if ((isset($_POST['listvalue'])) && ($_POST['listvalue']=='eight')) echo "selected" ?>>8</option>
 
yep,i got it right...what a slight change was required. :)