Solved

PHP: Form validation, COOKIES, RegEx and labeled addresses

Posted on 2007-03-19
3
202 Views
Last Modified: 2008-03-06
I have a rather complicated address form that shows the value based on cookies being set or not. Currently if someone has been to my site and saves their addresses they are called back and displayed as an option to choose for shipping (i.e. - Work address, home address) when they log in. I also give them the option to enter a new address with a new label (i.e. - Vacation home address, etc).

My problem is when someone forgets to enter a required field or enters the info incorrectly and submits, the info is wiped out and they need to re-enter everything. My billing page did the same thing, but got that sorted via some great Experts and need some additional assistance.

Here is a snippet of my code:

<input type="text" name="ship_add_1[New]" value="<?php echo $rsShip[ship_add_1]; ?>" />

or

<select name="ship_country[New]" onchange="doUSIntl(this);">
<option value="US" >United States</option>
</select>

Where "[New]" is used to differentiate between the newly entered data and that pulled from the database as shown here:

$addrLabel = $row['shipLabel'];
...
print '<input type="hidden" name="ship_add1[' . $addrLabel . '] value="' . $row['ship_add1'] . '">';

and is submitted to:

$index = $_POST['choose_add'];
### where $_POST['choose_add'] is a radio selection with value of the respective ship label ###
if (isset($_POST['ship_add1'][$index])) { // regex cut for brevity
     $s_add1 = stripslashes($_POST['ship_add1'][$index]);
}


All this being said, I need a way if the person misses a required field or for some reason gets returned false by form validation it doesn't wipe out their data. Also, I use the if(isset(...)) if a cookie is set if they get to the end of the checkout and want to go back and edit the info they don't have to re-enter all their data. I also don't know how to get the drop down to auto select either.

Ideas?

~Aus2Srq
0
Comment
Question by:Aus2Srq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Accepted Solution

by:
under_dog earned 500 total points
ID: 18749608
Well to retain the submitted information you have 2 options:

1/ Save all the data from the form in session variables before you do the validation. If there's a problem you can redirect back to the form and then populate the fields with the values stored in your session.

2/ Process the form on the same page ... that way you still have access to the $_POST variables to repopulate your form with if validation fails.

As for how to have your dropdown select a value do:

<select name="ship_country[New]" onchange="doUSIntl(this);">
<option value="US" <?php if ($selectedvalue == "US") { echo ' selected'; } ?>>United States</option>
</select>
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

627 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