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?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

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
You are doing this:

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

But it should just be:
<option selected>B</option>
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Man, Dave beat me by 45 seconds.
Dave BaldwinFixer of ProblemsCommented:
@gr8gonzo, it was my turn...
Ray PaseurCommented:
For reference, these pages are usually fairly good and they have examples.
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>
rgranlundAuthor Commented:
If you goto: fill out the form and use an email like 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.
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 =""
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>
Dave BaldwinFixer of ProblemsCommented:
You must have changed something because I don't find 'selected' in the source code at all right now.
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

Chris StanyonWebDevCommented:
You have a 2 different state dropdowns on that page - the first one is wrong, the second one is right.
rgranlundAuthor Commented:
All good.  Somewhere along the way the page got cached!  Thanks for all of the help!
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.
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

From novice to tech pro — start learning today.