Link to home
Start Free TrialLog in
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
Avatar of rbudj
rbudj
Flag of United States of America image

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.
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>
Avatar of steva
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:

User generated image
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?
ASKER CERTIFIED SOLUTION
Avatar of rbudj
rbudj
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of steva

ASKER

Got it!

Thanks.  I gave you the points.
You are welcome. Glad I could help!