Re: recommended way for server side validation - include in same file or have a different file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello Vinayakam,

Tuesday, March 8, 2005, 4:11:43 AM, you wrote:
V> However in the second case, in case of an error, displaying the
V> data becomes an issue.

V> Any recommended ways for server side validation


I usually do the updates in the same file unless there are a lot of
different things going on (inserts, updates, deletes etc), then I'll
break it out into separate files. However, I do almost always have an
included file with all my error checking code.

So, let's say in the main page(let's call it index.php) where we're
filling out a form, we have the following three items:

formFirstName
formLastName
formEmployeeNumber

We submit and the action is PHP_SELF

At the top of index.php is an include for errorhandler.php which only
happens if $_POST['submit'] == "Submit"

errorhandler.php includes functions.php at the top and
errorhandler.php looks like this:

validateString('firstName', $_POST['formFirstName'], 25)
validateString('lastName', $_POST['formLastName'], 25)
validateNumeric('EmployeeNumber', $_POST['formEmployeeNumber'], 5)


functions.php contains all my commonly used functions, and
validateString() might look like this:

function validateString($fieldName, $fieldValue, $size) {
  global $errors[];
  
  if ($strlen($fieldValue) <= $size) {
    if (does not contain any undesirable characters) {
      $errors['$fieldname'] = 0;
    }
    else {
      $errors[0] = "1";
      $errors[$fieldName] = "You may only use letters and hyphens.";
    }
  }
  else {
    $errors[0] = "1";
    $errors[$fieldName] .= "This field is limited to $size characters.";
  }
  return;
}

Then back in index.php I test $errors[0] to see if there was an error,
if so, I skip over the insert, update or delete and just go back down
to the form and display the error where appropriate.

Note: I didn't test the above code for this explanation.

It gets a bit harder when you have separate files but it's doable.



-- 
Leif (TB lists moderator and fellow end user).

Using The Bat! 3.0.2.3 Rush under Windows XP 5.1
Build 2600 Service Pack 2 on a Pentium 4 2GHz with 512MB

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux