Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP signup form validation

I have this simple form on submit it will be redirected to submit.php, if there are errors it shows error messages on submit.php. Now what I want that the error messages will be shown back to form page.

<html>
<head>
<? require_once('lib.php'); ?>
</head>
<body>
<form name="my-form" id="my-form" method="post" action="submit.php">
        Your name:
        <input name="name" value="" size="30" maxlength="255" />
        Your email:
        <input name="email" value="" size="30" maxlength="255" />
        Your favourite color:
            <select name="fav_color">
                <option value="">-select please-</option>
                <option value="Black">Black</option>
                <option value="White">White</option>
                <option value="Blue">Blue</option>
                <option value="Red">Red</option>
                <option value="Yellow">Yellow</option>
            </select>
        Your comment:
        <textarea name="comment" rows="6" cols="35"></textarea>
    <input type="submit" value="Submit" />         
</form>
</body>
</html>
<?php

require_once('lib.php');

function getErrors($postData,$rules){

  $errors = array();

  // validate each existing input
  foreach($postData as $name => $value){

    //if rule not found, skip loop iteration
    if(!isset($rules[$name])){
        continue;       
    }

    //convert special characters to HTML entities
    $fieldName = htmlspecialchars($name);

    $rule = $rules[$name];

    //check required values
    if(isset($rule['required']) && $rule['required'] && !$value){
        $errors[] = 'Field '.$fieldName.' is required.';
    }

    //check field's minimum length
    if(isset($rule['minlength']) && strlen($value) < $rule['minlength']){
         $errors[] = $fieldName.' should be at least '.$rule['minlength'].' characters length.';    
    }

    //verify email address     
    if(isset($rule['email']) && $rule['email'] && !filter_var($value,FILTER_VALIDATE_EMAIL)){
      $errors[] = $fieldName.' must be valid email address.';
    }

    $rules[$name]['found'] = true;

  }


  //check for missing inputs
  foreach($rules as $name => $values){
    if(!isset($values['found']) && isset($values['required']) && $values['required']){
      $errors[] = 'Field '.htmlspecialchars($name).' is required.';
    }

  }

  return $errors;
}

$errors = getErrors($_POST,$validation_rules);

if(!count($errors)){
  echo 'Your form has no errors.';
}
else{  
  echo '<strong>Errors found in form:</strong><ul><li>';
  echo join('</li><li>',$errors);
  echo '</li></ul><p>Correct your errors and try again.</p>';
}
?>

As this php code display the error messages on same page. I want to display that error messages back on the form.php page. Does anyone help me to do so..

like image 499
ejaz Avatar asked Oct 07 '22 04:10

ejaz


1 Answers

This article describes your solution.

You should create a validator script(for example validate.php) and submit the form there for validation. If the validation fails, validator script should return a (JSON, XML, whatever you want) array of validation errors. Else - return a redirect link.

So when you click "submit" on your form an AJAX request to validator.php should happen, not a redirect.

You should consider using a framework for such problems. It would save a lot of coding time.

like image 137
Sergey Eremin Avatar answered Oct 13 '22 00:10

Sergey Eremin