Solved

PHP: Form validation, COOKIES, RegEx and labeled addresses

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
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 …

734 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