<?php /** * Created with CssTemplateHeaven Form Builder * www.csstemplateheaven.com/tools/form_builder * www.csstemplateheaven.com */ $form = new ProcessForm(); $form->field_rules = array('name' => 'required', 'tel' => 'required', 'email' => 'email|required', 'message' => 'required'); $form->validate(); class ProcessForm { public $field_rules; public $error_messages; public $fields; private $error_list; private $is_xhr; function __construct() { $this->error_messages = array('required' => 'This field is required', 'email' => 'Please enter a valid email address', 'number' => 'Please enter a numeric value', 'url' => 'Please enter a valid URL', 'pattern' => 'Please correct this value', 'min' => 'Please enter a value larger than the minimum value', 'max' => 'Please enter a value smaller than the maximum value'); $this->field_rules = array(); $this->error_list = ''; $this->fields = $_POST; $this->is_xhr = $this->xhr(); } function validate() { if (!empty($this->fields)) { //Validate each of the fields foreach ($this->field_rules as $field => $rules) { $rules = explode('|', $rules);
} public function postFiles($poststr, $input_val = '') { return isset($_FILES[$poststr]["name"]) ? strip_tags($_FILES[$poststr]["name"]) : htmlspecialchars($input_val); } public function message($msg = '') { //if no errors are found if (count($this->errorinfo) == 0 && $this->successflag == true) { $this->successmsg .= $msg; } else { // Display Error Msg Here if (count($this->errorinfo) == 1) { $this->successflag = false; $msg = "There is " . count($this->errorinfo) . " error in the form field"; } elseif (count($this->errorinfo) > 1) { $this->successflag = false; $msg = "There are " . count($this->errorinfo) . " errors in the form fields"; } } return $this->message = $this->successflag == true ? "<p class=\"successmsg\">" . $this->successmsg . "</p>" : "<p class=\"error\">" . $msg . "</p>"; } /* This function validate input forms */ static function validate() { return new FormValidator(); } } $process = new ProcessForm(); $validate = ProcessForm::validate();
<?php include_once "../lib/buildform.class.php"; /** * How to use the from class: implement it like this */ try { /** Process Forms and Validate Errors **/ $process = new ProcessForm(); $validate = $process->validate(); if ($process->submitForm()) { $required = array("firstname", "lastname", "username", "email"); $process->errorinfo = array_merge($process->errorinfo, $validate->check_requiredFields($required)); $check_invalidchars = array("firstname", "lastname", "username"); $process->errorinfo = array_merge($process->errorinfo, $validate->check_invalidChars($check_invalidchars)); $requiredlen = array("firstname" => 50, "lastname" => 50, "username" => 30); $process->errorinfo = array_merge($process->errorinfo, $validate->check_FieldLength($requiredlen)); $selectedindex = array("gender" => "--Select Your Gender--"); $process->errorinfo = array_merge($process->errorinfo, $validate->check_selectField($selectedindex)); //validate password array_push($required, "password"); $process->errorinfo = array_merge($process->errorinfo, $validate->check_PasswordLength(array("password" => 6))); $process->errorinfo = array_merge($process->errorinfo, $validate->check_requiredFields($required)); $process->errorinfo = array_merge($process->errorinfo, $validate->check_PasswordFields("password", "cpassword")); /** Insert Data into Database **/ $process->message("You have successfully registered {$process->post("firstname")}"); if ($process->successflag) { echo "<p style='background:white;text-align:center'>Hooray your form is ready to be inserted into the database :)</p>"; //echo "Form_submitted - ".$process->post("firstname"); //$rec::$tablefields = array('fieldname1'=>'?','fieldname2'=>'?',...); //array("firstname","lastname","gender","username","email","password","cpassword",); //$sql = $query->getTable("users");