开发者

How to display form validation errors?

开发者 https://www.devze.com 2023-01-14 14:28 出处:网络
How can I populate and display the $errors variable only after the form is submitted and there is an error?

How can I populate and display the $errors variable only after the form is submitted and there is an error?

<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
  <?php
    if(!is_null($errors)) {
        echo "<h3>$errors</h3>";
    }
  ?>

  <label for="Name">Name</label><em>(Required)</em>
  <input type="text" name="Name"/>

  <label for="Email">Email</label><em>(Required)</em>
  <input type="text" name="Email"/>

  <label for="Website">Website</label>
  <input type="text" name="Website"/>

  <label for="Message">Message</label><em>(Required)</em>
  <textarea name="Message"></textarea>

  <button id="submit" type="submit" value="submit"name="submit">Submit</button>
  <input type="hidden" name="Submit" />
</form>

and my script to handle the form

$errors = null;

if(isset($_POST['submit'])) {   

  if(!empty($_POST['Name']) && !empty($_POST['Email']) && !empty($_POST['Message'])) {

    $name = $_POST['Name'];
    $email = $_POST['Email'];
    $website = $_POST['Website'];
    $message = $_POST['Message'];

    //strip any html tags and slashes off of the fields
    $nameClean = strip_ta开发者_JS百科gs($name);
    $emailClean = strip_tags($email);
    $websiteClean = strip_tags($website);
    $messageClean = strip_tags($message);
    $messageCleanAndStripped = stripslashes($messageClean);
  }

} else {
    $errors = "Please fill in the required fields";
}


$errors will be displayed even when you load the page for the first time, because that's what your code does:

if(isset($_POST['submit'])) 
{
  ....
} 
else 
{
    $errors = "Please fill in the required fields";
}

It means if the user loads the page the first time, he hasn't posted anything, so isset($_POST['submit']) is false, then you assign an error message to the $errors variable. You should change your code to this:

if(isset($_POST['submit'])) {   

  if(!empty($_POST['Name']) && !empty($_POST['Email']) && !empty($_POST['Message'])) {

    $name = $_POST['Name'];
    $email = $_POST['Email'];
    $website = $_POST['Website'];
    $message = $_POST['Message'];

    //strip any html tags and slashes off of the fields
    $nameClean = strip_tags($name);
    $emailClean = strip_tags($email);
    $websiteClean = strip_tags($website);
    $messageClean = strip_tags($message);
    $messageCleanAndStripped = stripslashes($messageClean);
  }
  else 
  {
    $errors = "Please fill in the required fields";
  }

} 

Just insert the else statement inside the condition, so you'll be checking for errors only when the user actually submitted something.

Hope it helps.


You have if-else wrongly matched. Do it like this:

if(isset($_POST['submit']))
{   
  if(!empty($_POST['Name']) && !empty($_POST['Email']) && !empty($_POST['Message']))
  {
    $name = $_POST['Name'];
    $email = $_POST['Email'];
    $website = $_POST['Website'];
    $message = $_POST['Message'];

    //strip any html tags and slashes off of the fields
    $nameClean = strip_tags($name);
    $emailClean = strip_tags($email);
    $websiteClean = strip_tags($website);
    $messageClean = strip_tags($message);
    $messageCleanAndStripped = stripslashes($messageClean);
  }
  else
  {
      $errors = "Please fill in the required fields";
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号