Solved

Restoring a default drop down value

Posted on 2012-03-18
6
216 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

706 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