Solved

PHP: Form validation, COOKIES, RegEx and labeled addresses

Posted on 2007-03-19
3
191 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

760 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

20 Experts available now in Live!

Get 1:1 Help Now