Avatar of Aus2Srq
Aus2Srq

asked on 

PHP: Form validation, COOKIES and RegEx

I have a form in my shopping cart and once submitted goes through regex check to prevent sql injection. My question begins when any portion of form validation returns false it wipes out all the data and the form returns with blank fields (very frustrating for end user).

Additional information about validation; if it the validation through fine it stores the data in a cookie. When the person gets to the end of checkout and if they entered wrong info in their address I have set an Edit link to take them back to either Billing or Shipping page to correct. This data is read back in via the cookie and loads the cookie data into form make easier to correct.

My question is this, how can I keep the functionality of reading in cookie data if the customer is Editing previously entered information but also prevent the new data (not stored in cookie yet as it fails in regex) from being wiped when returned false in the validation?

~Aus2Srq
PHP

Avatar of undefined
Last Comment
Aus2Srq
Avatar of glcummins
glcummins
Flag of United States of America image

If you are able to use a session, you can store the form information into session variables

if (<validation of name supplied in the form>)
{
   $_SESSION['suppliedName'] = $name;
}

Then, in the form itself, set each input field to check if the value exists in the $_SESSION variable:

<input type="text" name="name" value="<?php
if ($_SESSION['suppliedName'] != "")
{
   echo $_SESSION['suppliedName'];
}
?>" />

This will populate the input field with the previously supplied value, if it exists.
Avatar of steelseth12
steelseth12
Flag of Cyprus image

If the validation is on the same page as the form just do

for textbox
<input type="text" name"fieldname" value="<?=$_POST["fieldname"]?>">

for textarea
<textarea name="textarea">$_POST["textareaname"]</textarea>

for select
<select name="list">
<option value="option1" <?=($_POST["list"]=="option1"?"selected":"")?>>option1</select>
<option value="option2" <?=($_POST["list"]=="option2"?"selected":"")?>>option2</select>
</select>

for checkbox
<input type="checkbox" name="checkboxname" value="1" <?=($_POST["checkboxname"]==1?"checked":"")?>>

Now if the validation is done on a different page and then returned to the form follow glcummins method.
Also if you use get instead of post replace $_POST with $_GET

Avatar of Aus2Srq
Aus2Srq

ASKER

Here comes the hard part steelseth, Your solution works except for the fact that I am already using the value field for if the cookie is set.

<input type="text" name="email_customer" value="<?php if(isset($rsBill[email])) {echo $rsBill[email];} ?>" />

Any ideas on how to do both?

~A2S
Avatar of steelseth12
steelseth12
Flag of Cyprus image

<input type="text" name="email_customer" value="<?=(isset($rsBill[email])?$rsBill[email]:$_POST["fieldname"])?>" />
ASKER CERTIFIED SOLUTION
Avatar of glcummins
glcummins
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of glcummins
glcummins
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of steelseth12
steelseth12
Flag of Cyprus image

Im going to shot myself in the head
Avatar of Aus2Srq
Aus2Srq

ASKER

sorry steelseth, I meant to click on yours too as the assisted solution, but clicked things too quickly. I do have a continuation of this question that I will be reposting that's even more complicated.

~A2S
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo