PHP按顺序显示错误消息并重新显示正确的字段

Mos*_*aic 2 php forms

我有一个电子邮件表单,检查三个字段,名称,有效的电子邮件和评论.但是它现在的设置方式,因为名称和注释在一个函数中,它首先检查名称和注释,即使电子邮件无效,我如何重新编写它以便按顺序检查字段.此外,我想重新显示没有错误的字段,因此用户不必再次键入.请帮忙.谢谢

<?php
$myemail = "comments@myemail.com";
$yourname = check_input($_POST['yourname'], "Enter your name!");
$email = check_input($_POST['email']);
$phone = check_input($_POST['phone']);
$subject = check_input($_POST['subject']);
$comments = check_input($_POST['comments'], "Write your comments!");

if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
  {
    show_error("Enter a valid E-mail address!");
  }

exit();

function check_input($data, $problem='')
 {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
    show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<!doctype html>
<html>
<body>
<form action="myform.php" method="post">
 <p style="color: red;"><b>Please correct the following error:</b><br />
 <?php echo $myError; ?></p>
 <p>Name: <input type="text" name="yourname" /></P>
 <P>Email: <input type="text" name="email" /></p>
 <P>Phone: <input type="text" name="phone" /></p><br />
 <P>Subject: <input type="text" style="width:75%;" name="subject" /></p>
 <p>Comments:<br />
 <textarea name="comments" rows="10" cols="50" style="width: 100%;"></textarea></p>
 <p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
<?php
exit();
}
?>
Run Code Online (Sandbox Code Playgroud)

dre*_*010 5

首先,我建议您一次验证所有字段,并在表单上显示所有相应的错误消息.主要原因是,如果他们必须提交一大堆表单,因为他们必须一次解决一个错误,这可能是糟糕的用户体验.我宁愿一次性更正我的电子邮件地址,密码,评论和选择,而不是一次修复一个,以揭示下一个错误是什么.

也就是说,这里有一些关于验证表单的指示.这通常是我如何处理表单做你想做的事情.这假设您的表单HTML和表单处理器(PHP)在同一个文件中(这就是您现在拥有的).你可以拆分这两个,但这样做的方法可能有点不同.

  • 有一个功能或代码块输出表单并知道您的错误消息,并可以访问以前的表单输入(如果有).通常,这可以保留在函数之外,并且可以是PHP脚本中的最后一段代码.
  • 为错误消息设置数组(例如$errors = array()).当此数组为空时,您知道提交没有错误
  • 在输出表单之前,检查表单是否在脚本顶部附近提交.
  • 如果表单已提交,请一次验证一个字段,如果字段包含错误,请将错误消息添加到$errors数组(例如$errors['password'] = 'Passwords must be at least 8 characters long';)
  • 要使用以前的值重新填充表单输入,您必须将输入的值存储在某处(您可以只使用$_POST数组,或者清理并将$_POST值分配给单个变量或数组.
  • 完成所有处理后,您可以检查是否存在任何错误,以确定此时是否可以处理表单,还是需要用户提供新输入.
  • 要做到这一点,我通常会做类似的事情 if (sizeof($errors) > 0) { // show messages } else { // process form }
  • 如果要重新显示表单,只需value=""向每个表单元素添加一个属性,并回显用户提交的值. 使用htmlspecialchars()或类似函数转义输出非常重要

有了这些东西,这里有一些表格的重新工作:

<?php
$myemail = "comments@myemail.com";
$errors  = array();
$values  = array();
$errmsg  = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    foreach($_POST as $key => $value) {
        $values[$key] = trim(stripslashes($value)); // basic input filter
    }

    if (check_input($values['yourname']) == false) { 
        $errors['yourname'] = 'Enter your name!';
    }

    if (check_input($values['email']) == false) {
        $errors['email'] = 'Please enter your email address.';
    } else if (!preg_match('/([\w\-]+\@[\w\-]+\.[\w\-]+)/', $values['email'])) {
        $errors['email'] = 'Invalid email address format.';
    }

    if (check_input($values['comments']) == false) {
        $errors['comments'] = 'Write your comments!';
    }

    if (sizeof($errors) == 0) {
        // you can process your for here and redirect or show a success message
        $values = array(); // empty values array
        echo "Form was OK!  Good to process...<br />";
    } else {
        // one or more errors
        foreach($errors as $error) {
            $errmsg .= $error . '<br />';
        }
    }
}

function check_input($input) {
    if (strlen($input) == 0) {
        return false;
    } else {
        // TODO: other checks?

        return true;
    }
}
?>
<!doctype html>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
 <?php if ($errmsg != ''): ?>
 <p style="color: red;"><b>Please correct the following errors:</b><br />
 <?php echo $errmsg; ?>
 </p>
 <?php endif; ?>

 <p>Name: <input type="text" name="yourname" value="<?php echo htmlspecialchars(@$values['yourname']) ?>" /></P>
 <P>Email: <input type="text" name="email" value="<?php echo htmlspecialchars(@$values['email']) ?>" /></p>
 <P>Phone: <input type="text" name="phone" value="<?php echo htmlspecialchars(@$values['phone']) ?>"/></p><br />
 <P>Subject: <input type="text" style="width:75%;" name="subject" value="<?php echo htmlspecialchars(@$values['subject']) ?>" /></p>
 <p>Comments:<br />
 <textarea name="comments" rows="10" cols="50" style="width: 100%;"><?php echo htmlspecialchars(@$values['comments']) ?></textarea></p>
 <p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我有一个更高级的例子,你可以在这里看到,也可以给你一些指导.

希望有所帮助.