5

Short version:

  • I have a form with over 100 fields and each field has server-side validation.
  • I have the form working and data being submitted successfully; I'm even redirecting them back to the form when there are errors with the validation, and displaying what error in specific happened.
  • When they're redirected back to the (massive) form, all of their data is gone and they need to type it again.

I have already thought of a couple ways of answering my own question, but they aren't the most elegant solutions out there.

My current thought process is: save all of their inputted data as $_SESSION variables -- and whenever they load the form, do an if statement around each input, as such;

if(isset($_SESSION['foo_data'])) {

    echo "<input type='text' name='foo' value='$_SESSION["foo_data"]'>";

} else {

    echo "<input type='text' name='foo' placeholder='Enter your text here...'>";

}

This method will work, but it also involves an immensely tedious definition of 140+ session variables, followed by an even more tedious and immense creating if/else statements around EVERY form field.

Is there any better way to do this?

Additional info: using plain php, no frameworks of any kind.

6
  • I would have an array() holding the form fields, then a loop, to build the form (with a check for session values)
    – user557846
    Jun 30, 2013 at 23:07
  • Why redirect if there are errors in the first place?
    – nice ass
    Jun 30, 2013 at 23:10
  • One Trick Pony: I'm currently doing validation in a separate PHP file all-together. Would you mind explaining how validating in the same file would change things? Jun 30, 2013 at 23:11
  • Keep your checks in the separate file. I'm just saying the you could display the form after checks fail, this way you keep submitted $_POST data and avoid use of sessions. No need for redirect unless the form is submitted successfully
    – nice ass
    Jun 30, 2013 at 23:17
  • Errr, doesn't that leave me in an identical situation? (Having to use 140 if / else statements for isset($_POST['foo_data'] to echo out different inputs, just the same as I would have to do with session variables). Jun 30, 2013 at 23:23

1 Answer 1

4

Sessions are unnecessary. Your basic plan should be:

  • Get variables from $_POST()
  • Validate form values.
  • Create an array with the values that passed validation.
  • Use the array to fill the values when redirected to the form.

Before I started using CodeIgniter, I used to use if statements to validate and generate error messages like this:

if ( ! empty($_POST['email'])){
    if ($this->verifyInput($_POST['email'], 6)){
        $fill['email'] = $_POST['email'];//$fill is the array of values that passed validation
    } else $tally++;//$tally keeps a running count of the total number of erors
}

I make a function call to $this->verifyInput which accepts the field value and the type of validation to perform--in this case it's 6, which indicates email. Here's my validation function:

function verifyInput($input, $type){
    if ($type == 0)     $pattern =  '/^1$/';//just the number 1
    elseif ($type == 1) $pattern =  '/^[0-9]+$/';//just integers
    elseif ($type == 2) $pattern =  '/^[A-Za-zÁ-Úá-úàÀÜü]+$/';//just letters
    elseif ($type == 3) $pattern =  '/^[0-9A-Za-zÁ-Úá-úàÀÜü]+$/';//integers & letters
    elseif ($type == 4) $pattern =  '/^[A-Za-zÁ-Úá-ú0-9àÀÜü\s()\/\'":\*,.;\-!?&#$@]{1,1500}$/';//text
    elseif ($type == 5) $pattern =  '/^[A-Za-zÁ-Úá-úàÀÜü0-9\']+[A-Za-zÁ-Úá-úàÀÜü0-9 \'\-\.]+$/';//name
    elseif ($type == 6) $pattern = '/^.+@[^\.].*\.[a-z]{2,}$/';//e-mail
    elseif ($type == 7) $pattern = '/^((\(0?[1-9][0-9]\))|(0?[1-9][0-9]))?[ -.]?([1-9][0-9]{3})[ -.]?([0-9]{4})$/';//brazilian phone
    if (preg_match($pattern, $input) == 1) return true;
    else return false;
}

Once the validation is done, I use the $fill array to prefill the form for each field:

<input class="field" type="text" name="email" value = "<?php echo $fill['email']; ?>"/>

You should also make sure that $fill is iniciated with empty values such as $fill['email']=''; or check to make sure the values are set before filling the input tags:

<input class="field" type="text" name="email" value = "<?php if(isset($fill['email'])) echo $fill['email']; ?>"/>

These are just general guidelines that you can adapt to your needs, but I found this to be a convenient way to handle it.

1
  • That works :) Thanks Pe de Leao! Out of curiosity (because I started learning it this week, and can't really tell how I like it yet) how do you like CodeIgniter VS flat PHP, in particular when it comes to forms & form validation? Jul 1, 2013 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.