Avatar of steva
steva
 asked on

Restoring a default drop down value

I have some PHP code that stores a buyer's information in a data base at the time of the sale. At that time I also store a cookie down in the browser that contains the orderId. When the buyer returns to the site I see the cookie, pull the buyer's info from the database, using that orderId,  and I prepopulate the pay form for them.  

The only problem is that the State/Territory is chosen from a drop down select menu on the pay form that has about 175 options.  I can tell from the database that the buyer is from, say Arizona, but there doesn't seem to be any way to set the drop down select element to show that value.  I'm just wonder if there is and I'm missing it.

Thanks
PHP

Avatar of undefined
Last Comment
rbudj

8/22/2022 - Mon
rbudj

One work around to this is to "display" the state instead of having it show up in the drop down box. In other words, When pulling the info from the database, have the state listed but not editable. Underneath that, you can have the drop down if they want to change it. I will continue to research options and let you know.
rbudj

here is another way.... take the state from the database and place it into a variable. On the page with the drop down menu, place the variable as the first option and add selected.

<?php
$state = $db['state'];
?>

<select>
<option value="<?php echo $state;?>" selected><?php echo $state;?></option>
</select>
steva

ASKER
Hi rbudj,

Thanks for responding.  I like your second idea, but I'm thinking that it may not completely there yet.  

The beginning of the select element now looks like below:

Select Statement
If I physically change the first select statement to what you have and they want to change it to Albania, the option won't be there anymore.  Also,  if this is the first time they're visiting the site, there won't be a cookie or a $state value from the db.  So I'm thinking that what I need is a conditional statement that echoes a new first option, set for the db $state, if I got the cookie.  Something like  this:

 <select id="countrySelect" name="country" class="input" style="width:190px;">
       <?php 
	if(cookie) {
	    echo "
		     <option value=$state> selected><$state</option>
                    ";
            }
	?>
        <option value="AD">&nbsp;&nbsp;Albania </option>
        <option value="DZ">&nbsp;&nbsp;Algeria </option>
        <option value="AS">&nbsp;&nbsp;American Samoa </option>
        <option value="AD">&nbsp;&nbsp;Andorra </option>

Open in new window


I'll still have to come up with an abbreviation for the state/province for the first variable, instead of just $state, but I could handle that with a table.

Another concern is how the select will work with two options selected, since the default that's hard coded will still be there.  Will the select always pick the first "selected" and ignore any others?  I suppose I could put conditional PHP down there too to remove that selected if I have a cookie.

What do you  think?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
rbudj

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
steva

ASKER
Got it!

Thanks.  I gave you the points.
rbudj

You are welcome. Glad I could help!