PHP: Generic form processing script


/ Published in: PHP
Save to your folder(s)

A handy, generic form processor which validates presence of fields marked in a hidden value, like: . Easy to override and customize as needed.

Adapted from a script found in PHP 5 Unleashed.


Copy this code and paste it in your HTML
  1. /**
  2.  * ==============================================
  3.  * Generic form processing code
  4.  * ==============================================
  5. */
  6. $form_errors = array();
  7. $form_errorlist = false;
  8.  
  9. /**
  10.  * Adds an error to our $form_errors variable
  11.  *
  12.  * @return void
  13.  **/
  14. function add_error($error)
  15. {
  16. global $form_errorlist, $form_errors;
  17. $form_errorlist = true;
  18. $form_errors[] = $error;
  19. }
  20.  
  21. /**
  22.  * Called by validate_form
  23.  * @param $method
  24.  * the container holding our form data $_GET or $_POST
  25.  * @return void
  26.  **/
  27. function _process_form($method)
  28. {
  29. if (function_exists("process_form")) {
  30. $data = $method;
  31.  
  32. // clean up any elements not included within
  33. foreach($data as $key => $val) {
  34. if (preg_match("/(submit|required)|(_desc$)/i", $key) == 1) {
  35. unset($data[$key]);
  36. }
  37. }
  38. // call process_form(), pass clean form values
  39. process_form($data);
  40. }
  41. }
  42.  
  43. /**
  44.  * Generic validation steps
  45.  *
  46.  * @return bool
  47.  **/
  48. function validate_form($method)
  49. {
  50. $process = true; // assume true until proven otherwise
  51. if (!isset($method['required'])) {
  52. add_error("Required hidden element 'required' missing.");
  53. $process = false;
  54. } else {
  55. $required_fields = explode(',', $method['required']);
  56.  
  57. // Scan for empties
  58. foreach($required_fields as $val) {
  59. if (empty($method[$val])) {
  60. // Required element is missing data
  61. if (isset($method[$val . "_desc"])) {
  62. $errormsg = "The required field '" . $method[$val . "_desc"] . "' was empty.";
  63. } else {
  64. $errormsg = "The required field '$val' was emtpy.";
  65. }
  66. add_error($errormsg);
  67. $process = false;
  68. }
  69. }
  70.  
  71. // Call dynamic validator methods
  72. foreach($method as $key => $val) {
  73. if (preg_match("/(submit|required)|(_desc$)/i", $key) != 1) {
  74. $validator_function = $key . "_validate"; // the name of the function which will be called for validation
  75. if (function_exists($validator_function)) {
  76. if (!isset($method[$key . "_desc"])) {
  77. $result = $validator_function($val, $key);
  78. } else {
  79. $result = $validator_function($val, $method[$key . "_desc"]);
  80. }
  81.  
  82. if ($result !== true) {
  83. add_error($result);
  84. $process = false;
  85. }
  86. }
  87. }
  88. }
  89. }
  90.  
  91. if ($process) {
  92. _process_form($method);
  93. return true;
  94. }
  95. return false;
  96. }
  97.  
  98. /**
  99.  * Returns the value stored in $method or an alternate value when not available
  100.  * @param $field_name
  101.  * The name value of the input element
  102.  * @param $alternate
  103.  * Text to be displayed if value field_name value is undefined
  104.  * @return String
  105.  * @author Nate Miller
  106.  **/
  107. function write_form_var($field_name, $alternate)
  108. {
  109. global $method;
  110. $out = $alternate;
  111. if ($method[$field_name]) {
  112. $out = $method[$field_name];
  113. }
  114. return $out;
  115. }
  116.  
  117. /**
  118.  * ==============================================
  119.  * Custom validation logic
  120.  * ==============================================
  121. */
  122.  
  123. $method = &$_GET;
  124. if (isset($method['submit'])) {
  125. validate_form($method);
  126. }
  127.  
  128. /**
  129.  * Validates an email address
  130.  * @param $data
  131.  * the submitted value
  132.  * @param $desc
  133.  * the field descriptor - human friendly name of failed field
  134.  *
  135.  * @return bool | error string
  136.  * @author Nate Miller
  137.  **/
  138. function email_validate($data, $desc)
  139. {
  140. $email_reg = "/^[a-z0-0\._-]+@+[a-z0-0\.-]+\.+[a-z]{2,3}$/i";
  141. if (preg_match($email_reg, $data) != 1)
  142. return "The '$desc' field in invalid.";
  143. return true;
  144. }
  145.  
  146. /**
  147.  * Controls called by validate_form when form is sucessful
  148.  *
  149.  * @return void
  150.  * @author Nate Miller
  151.  **/
  152. function process_form()
  153. {
  154. echo "Submit dude";
  155. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.