【问题标题】:Php Form - Error Checking and outputPHP 表单 - 错误检查和输出
【发布时间】:2011-07-11 14:42:18
【问题描述】:

我创建了一个简单的表单,其中包含一些必填字段,如果未填写,则会将错误返回给用户,通知他们该字段是必填的。 由于有多个检查字段,它可以输出多个错误消息。

我想知道如何在我的表单上定义一个显示这些错误的区域,因为目前这些错误只是显示在表单的底部,除非你向下滚动页面,否则你看不到。

我可以定义我的错误显示在哪里吗?

这里是错误检查代码:编辑

  Old code was here

过去有人建议我创建一个循环来一次检查一个错误,但我是 php 新手,所以不知道该怎么做。

    $errors = '';
    if(empty($_POST['studentName'])) 
    {
    $errors .= "You did not enter the student name<br/>";
    }

//Code to check that the Tutor Name field is completed
    if(empty($_POST['tutorName'] ))
    {
    $errors .="You did not select a tutor<br/>";
    }

//Code to check that the Procedure field is completed
    if(empty($_POST['procedure'] ))
    {
    $errors .="You did not enter a procedure<br/>";
    }
//Code to check that the Grade field is completed
    if(empty($_POST['grade'] ))
    {
    $errors .="You did not enter a grade<br/>";
    }
//Code to check that the Student Reflection field is completed
    if(empty($_POST['studentReflection'] ))
    {
    $errors .="You did not enter a reflection<br/>";
    }
//Code to check if the tick box is checked that the tutor comment is entered
    if( !strlen($_POST['tutorComments']) && isset($_POST['alert'] ))
    {
    $errors .="You must enter a reasan why you ticked the alert box";
    }

//Code to check the password field is completed and correct
    if (empty($_POST['password']))
    {
    $errors .="You did not enter you password";
    }

    if (!empty($_POST['password']))
    {

//==========================================
//  ESCAPE DANGEROUS SQL CHARACTERS
//==========================================
function quote_smart($value, $handle) {

   if (get_magic_quotes_gpc()) {
       $value = stripslashes($value);
   }

   if (!is_numeric($value)) {
       $value = "'" . mysql_real_escape_string($value, $handle) . "'";
   }
   return $value;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

    $masterpass = $_POST['password'];
    $masterpass = htmlspecialchars($masterpass);

    //==========================================
    //  CONNECT TO THE LOCAL DATABASE
    //==========================================
    $user_name = "username";
    $pass_word = "password";
    $database = "name of database";
    $server = "server";

    $db_handle = mysql_connect($server, $user_name, $pass_word);
    $db_found = mysql_select_db($database, $db_handle);

    if ($db_found) {

        $masterpass = quote_smart($masterpass, $db_handle);

        $SQL = "SELECT * FROM masterpass WHERE password = $masterpass";
        $result = mysql_query($SQL);
        $num_rows = mysql_num_rows($result);

    //====================================================
    //  CHECK TO SEE IF THE $result VARIABLE IS TRUE
    //====================================================

        if ($result) {
            if ($num_rows > 0) {
                echo "";
            }
            else {
                $errors .= "Password was not recognised";
                exit();
            }   
        }
        mysql_close($db_handle);

    }
}   
if(!empty($errors))
{
    echo '<div class="errors">' . $errors . '</div>';
    exit();
    }   

}

【问题讨论】:

  • 请向我们提供此表单的完整 html 和 php,我可以提供帮助。仅给出执行错误检查的 php 部分并不能提供回答此问题所需的所有信息。
  • 如果您使用适当的缩进,您会发现开发和调试代码更容易。
  • 我建议使用某种形式的 javascript 直接向用户检查前端错误。仅使用 php 会变得非常混乱。

标签: php webforms


【解决方案1】:

你可以做类似的事情

$errors = '';

if(empty($_POST['studentName'])) 
{
  $errors .= "You did not enter the student name<br />";
}

if(empty($_POST['tutorName'] ))
{
  $errors .= "You did not select a tutor name.<br />";
}

// etc.

然后上面你的&lt;form&gt;

if (!empty($errors))
{
  echo '<div class="errors">' . $errors . '</div>';
}

使用 CSS 为 .errors 设置样式,这样它会更加突出。如果$errors 在您的应用程序逻辑中较高级为空白,则您可以执行通常的添加/更新到数据库并重定向到成功 页面。

【讨论】:

  • 感谢您的建议。我已经尝试过了,但忘了补充说我还有另一个要检查的密码字段。我已经更新了这个,但现在只有在用户输入密码时才会发生错误检查,如果他们把这个留空,它只是将数据提交到数据库而不检查。将更新我上面的代码,向您展示我现在拥有的内容
  • 提交后将验证检查移至表单检查下方,即在if ($_SERVER['REQUEST_METHOD'] == 'POST'){ 之后(另见@soylentplaid 的评论)。
  • 谢谢大家。在这些帖子的帮助下,我已经设法解决了
【解决方案2】:

echo()ing 你的错误是将它们添加到底部的原因。正如前面的答案所建议的那样,将它们分配给一个字符串并在定义的 div 中打印(如果它不为空)是专业人士的做法!

将错误定义为数组也可以,并允许您对错误过程进行更细粒度的控制。

$errors = array();
if(empty($_POST['studentName'])) 
    $errors[] = "You did not enter the student name";

if(empty($_POST['tutorName'] ))
    $errors[] = "You did not select a tutor name.";

//etc...

//At the top of your page.
if (sizeof($errors)>0) {
    echo "<div class='errordiv'>";
    echo "<ul>";
    foreach ($errors as $err) {
        echo "<li>".$err."</li>"; //or whatever format you want!
    }
    echo "</ul></div>";
}

您还可以将错误数组作为参数传递给其他函数,记录它们等。

【讨论】:

  • 您在错误检查块的末尾添加了以下内容:if (!empty($_POST['password'])) { 这会导致您的“密码是否填写错误检查”问题。您的其余代码在该块内执行。删除那些。此外,如果您在打印错误 div 后立即 exit(),则执行将在此处停止。我猜你还想显示你的表单!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-19
  • 1970-01-01
  • 2016-01-26
  • 2021-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多