Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Restoring a default drop down value

Posted on 2012-03-18
6
Medium Priority
?
258 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Accepted Solution

by:
rbudj earned 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses

885 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