Solved

PHP: Form validation, COOKIES, RegEx and labeled addresses

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

770 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