Solved

Restoring a default drop down value

Posted on 2012-03-18
6
223 Views
Last Modified: 2012-08-13
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
0
Comment
Question by:steva
  • 4
  • 2
6 Comments
 
LVL 16

Expert Comment

by:rbudj
ID: 37737341
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.
0
 
LVL 16

Expert Comment

by:rbudj
ID: 37737354
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>
0
 

Author Comment

by:steva
ID: 37738242
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?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 16

Accepted Solution

by:
rbudj earned 500 total points
ID: 37739258
<?php
$varState = "This Country";
?>

<select>
<?php
if ($varState != ""){
echo "<option value=\"". $varState ."\" selected>" . $varState . "</option>";
}
?>
      <option value="AD">Albania</option>
      <option value="DZ">Algeria</option>

</select>

More code will be needed to keep the current value the same as currently in the database if the user decides not to change the option.

If there is no current database entry, then the echo statement will not be printed.
testPHP.php
testPHP.php
0
 

Author Closing Comment

by:steva
ID: 37739731
Got it!

Thanks.  I gave you the points.
0
 
LVL 16

Expert Comment

by:rbudj
ID: 37739755
You are welcome. Glad I could help!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now