php select option show chossen

I have a dynamically generated form that has a select option.  One of the options is to choose a state.  There are two different forms that can bring you to this specific page.  On the page is this option within a select:

echo '<option '; if(isset($_POST['bike_state']) && $_POST['bike_state'] == $row->state) {echo 'selected="selected"'; } elseif ( $bike_state  == $row->state ) {
								 echo 'selected="selected"'; } else { echo 'selected=""'; }
								echo ' class="option-center" value="'.$row->state.'" >'.$row->state.'</option>';		

Open in new window


It works fine.  It always shows the appropriate "selected" in the code, however, on screen it is not showing the selected choice.  How do I fix that?
LVL 7
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
The general design goes something like this...

As soon as the client makes a selection, you record it in the database table that is the model for the selections.  If the client changes the selection, you update the model, so that the database table always represents the clients most recent selection.

Then when you go to another page, or regenerate the same form on the same page, you query the database to get the prior selections, and you prepopulate the form with the information you remembered in the database.  If there is nothing in the database, you provide a reasonable "default" value.  If there is anything in the database, you know you're working with the client's current selection and you can prepopulate the <option> tag with "selected" to reflect the client's earlier choice.
0
Dave BaldwinFixer of ProblemsCommented:
I think you need to remove the 'else'.  Some browsers will consider the word 'selected' to mean that item is selected.  If you have it in all of the option items, it may not work as you expect.
// remove this part
 else { echo 'selected=""'; }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
You are doing this:

<select>
<option selected="">A</option>
<option selected="selected">B</option>
<option selected="">C</option>
</select>

But it should just be:
<select>
<option>A</option>
<option selected>B</option>
<option>C</option>
</select>
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

gr8gonzoConsultantCommented:
Man, Dave beat me by 45 seconds.
0
Dave BaldwinFixer of ProblemsCommented:
@gr8gonzo, it was my turn...
0
Ray PaseurCommented:
For reference, these pages are usually fairly good and they have examples.
http://www.w3schools.com/tags/tag_option.asp
http://www.w3schools.com/tags/att_option_selected.asp
0
Chris StanyonWebDevCommented:
Further to the other Expert's comments, you have 2 choices when selecting an option.

If your DOCTYPE is HTML (4/5), then it's this:

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

If it's XHTML, then it's this:

<option selected="selected" value="something">Something</option>
0
rgranlundAuthor Commented:
If you goto: http://23.101.151.179/ fill out the form and use an email like test@test.com and submit.  You will see the new form and about 1/4 the way down there is a place for State.  If you view the code you will see the state selected but not shown.
0
Chris StanyonWebDevCommented:
Works for me in Firefox, although you're still using the selected attribute wrong. Currently you have this:

<option class="option-center" value="CA" selected="">CA</option>

What you should have is this:

<option class="option-center" value="CA" selected>CA</option>

Note that selected is on it own - it has no =""
0
gr8gonzoConsultantCommented:
Works for me, too. Also, make sure you are separating out attributes properly, so you don't have letters next to quote marks:

<option selected="selected"class="option-center" value="AZ" >AZ</option>
                                              ^^^
0
Dave BaldwinFixer of ProblemsCommented:
You must have changed something because I don't find 'selected' in the source code at all right now.
0
rgranlundAuthor Commented:
There is no = in my code.
{
								echo '<option '; if(isset($_POST['bike_state']) && $_POST['bike_state'] == $row->state) {echo 'selected'; } elseif ( $bike_state  == $row->state ) {
								 echo 'selected'; }
								echo ' class="option-center" value="'.$row->state.'" >'.$row->state.'</option>';		
							}

Open in new window

0
Chris StanyonWebDevCommented:
You have a 2 different state dropdowns on that page - the first one is wrong, the second one is right.
0
rgranlundAuthor Commented:
All good.  Somewhere along the way the page got cached!  Thanks for all of the help!
0
gr8gonzoConsultantCommented:
The comment of mine that you selected as an assist wasn't really adding anything new. Dave had already covered the solution. Unless you disagree, let me know and I can remove the points assignment and you can allocate them all to Dave's comment.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.