Link to home
Start Free TrialLog in
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
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.
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
<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

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Im going to shot myself in the head
Avatar of 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