Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-表单错误处理问题_Php_Html_Forms_Error Handling - Fatal编程技术网

PHP-表单错误处理问题

PHP-表单错误处理问题,php,html,forms,error-handling,Php,Html,Forms,Error Handling,如果不满足某些条件,我将尝试创建错误消息。因此,用户填写表单,如果字段为空或未通过我的验证,则返回错误消息 表格如下: if (isset($_POST)) { if (checkEmail($email) == TRUE && $name != NULL && $surName != NULL) { mysql_query( "INSERT INTO USR_INFO (NAME, MAIL, SURNAME)

如果不满足某些条件,我将尝试创建错误消息。因此,用户填写表单,如果字段为空或未通过我的验证,则返回错误消息

表格如下:

if (isset($_POST)) {
    if (checkEmail($email) == TRUE && $name != NULL && $surName != NULL) {    
        mysql_query(    "INSERT INTO USR_INFO (NAME, MAIL, SURNAME) 
                         VALUES ('$name', '$email','$surName') ") or die(mysql_error());
        header('Location: thanks.php');
    } 

    else {
        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">First Name</label>
                <input type="text" name="name" id="name" value="' .$_POST['name'].'" />
                <span class="required">&#42;</span>

                <label for="surName">Last Name</label>
                <input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="' .$_POST['email']. '" />
                <span class="required">&#42;</span>

                <input type="submit" name="submit" id="submit">
            </form>';
    }
} else {
        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">First Name</label>
                <input type="text" name="name" id="name" value="" />
                <span class="required">&#42;</span>

                <label for="surName">Last Name</label>
                <input type="text" name="surName" id="surName" value="" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="" />
                <span class="required">&#42;</span>


                <input type="submit" name="submit" id="submit">
            </form>';
}
并使用适当的消息将其添加到html表单字段:

$error[] = "Error Message";
现在我遇到的问题是,我只想在用户不满足条件时显示错误

if ($name == NULL) {$error[] = "Error Message";}
if ($surName == NULL) {$error[] = "Error Message 2";}
if (checkEmail($email) == FALSE || NULL) {$error[] = "Error Message 3";}
但我不能让它工作。当我尝试实现这个逻辑时,它会很好地解析页面,验证也会工作,但是如果我将必填字段留空,错误消息将不会显示。我的猜测是我没有正确地循环它

非常感谢您的帮助

编辑

我尝试了Frosty Z发布的答案,这就是我目前的答案:

    if (isset($_POST)) {
    $errorMessage = array();
        if ($name == '') { $errors[] = "Input name please." }
        if ($surName == '') { $errors[] = "Input last name please." }
        if (!checkEmail($email)) { $errors[] = "Email address not valid." }

    if (count($error) == 0) { 
            mysql_query(    "INSERT INTO USR_INFO (NAME, MAIL, SURNAME) 
                             VALUES ('$name', '$email', '$surName') ") or die(mysql_error());
            header('Location: thanks.php');
            exit;
    else {

        if (count($errors) > 0)
            echo "<p>Sorry, there are problems with the information you have provided:</p>";

        foreach($errors as $error)
            echo '<p class="error">'.$error.'</p>';

        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">Name</label>
                <input type="text" name="name" id="name" value="' .$_POST['name'].'" />
                <span class="required">&#42;</span>

                <label for="surName">Last name</label>
                <input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="' .$_POST['email']. '" />
                <span class="required">&#42;</span>

                <input type="submit" name="submit" id="submit">
            </form>';
    }
} else {
        echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">            
                <label for="name">Name</label>
                <input type="text" name="name" id="name" value="" />
                <span class="required">&#42;</span>

                <label for="surName">Achternaam</label>
                <input type="text" name="surName" id="surName" value="" />
                <span class="required">&#42;</span>

                <label for="email">E-mail</label>
                <input type="email" id="email" name="email" placeholder="example@domain.com" value="" />
                <span class="required">&#42;</span>

                <input type="submit" name="submit" id="submit">
            </form>';
}
if(isset($\u POST)){
$errorMessage=array();
如果($name=''){$errors[]=“请输入名称。”}
如果($姓氏=“”){$errors[]=“请输入姓氏。”}
如果(!checkEmail($email)){$errors[]=“电子邮件地址无效。”}
如果(计数($error)=0{
mysql\u查询(“插入USR\u信息(姓名、邮件、姓氏)
值(“$name”、“$email”、“$nam姓氏”)或die(mysql_error());
标题('Location:Thanke.php');
出口
否则{
如果(计数($errors)>0)
echo“对不起,您提供的信息有问题:

”; foreach($errors作为$error) 回显“

”.$error.

”;
echo'这里是对您的工作的一些重写,只需对错误消息进行最少的处理

BTW,您应该考虑采用一个合适的PHP框架,它将帮助您处理许多常见的开发任务。

$name = '';
$surName = '';
$email = '';

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

    $name = $_POST['name'];
    $surName = $_POST['surName'];
    $email = $_POST['email'];

    $errors = array();

    if ($name == '') { $errors[] = "Please type your name."; }
    if ($surName == '') { $errors[] = "Please type your surname."; }
    if (!checkEmail($email)) { $errors[] = "Wrong email format."; }

    if (count($errors) == 0) {
        // tip: use PDO or mysqli functions instead of mysql ones to bind variables.
        // currently there is a risk of SQL injection here
        mysql_query("INSERT INTO USR_INFO (NAME, MAIL, SURNAME) 
                     VALUES ('$name', '$email','$surName') ") or die(mysql_error());
        header('Location: thanks.php');
        exit;
    }
}

if (count($errors) > 0)
    echo '<p>Sorry, there are problems with the information you have provided:</p>';

foreach($errors as $error)
    echo '<p class="error">'.$error.'</p>';

echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">            
            <label for="name">First Name</label>
            <input type="text" name="name" id="name" value="'.htmlspecialchars($name).'" />
            <span class="required">&#42;</span>

            <label for="surName">Last Name</label>
            <input type="text" name="surName" id="surName" value="'.htmlspecialchars($surName).'" />
            <span class="required">&#42;</span>

            <label for="email">E-mail</label>
            <input type="email" id="email" name="email" placeholder="example@domain.com" value="'.htmlspecialchars($email).'" />
            <span class="required">&#42;</span>

            <input type="submit" name="submit" id="submit">
        </form>';
$name='';
$姓氏='';
$email='';
如果($\u服务器['REQUEST\u METHOD']='POST'){
$name=$_POST['name'];
$姓氏=$_POST['姓氏'];
$email=$_POST['email'];
$errors=array();
如果($name=''){$errors[]=“请键入您的姓名。”;}
如果($姓氏=“”){$errors[]=“请键入您的姓氏。”;}
如果(!checkEmail($email)){$errors[]=“电子邮件格式错误。”;}
如果(计数($errors)==0){
//提示:使用PDO或mysqli函数而不是mysql函数来绑定变量。
//目前,这里存在SQL注入的风险
mysql\u查询(“插入USR\u信息(姓名、邮件、姓氏)
值(“$name”、“$email”、“$nam姓氏”)或die(mysql_error());
标题('Location:Thanke.php');
出口
}
}
如果(计数($errors)>0)
echo'对不起,您提供的信息有问题:

; foreach($errors作为$error) 回显“

”.$error.

”; 回声' 名字 * 姓 * 电子邮件 * ';
你在哪里打印错误数组?最后你可以用
var_dump($error)
进行调试,你能试试if(isset($_POST['name'])而不是if(isset($_POST)),这可能会产生逻辑错误。举个例子,你能看看吗。
if(checkEmail($email)==FALSE | NULL)
不会像你想的那样工作。它相当于
如果((checkEmail($email)=FALSE)| | NULL)
,那么整个
| | NULL
部分是不必要的。你在echo中有一个echo检查表单action我试图实现你给我的内容,但页面没有解析。我的页面没有使用
$\u服务器['REQUEST\u METHOD']='POST'
也可以。我还检查了我是否正确嵌套了if和else,我觉得这似乎是正确的。@Perry:请参阅@MarioS:您收到了哪条错误消息?我已经测试过,没有任何解析问题。@FrostyZ“NetworkError:500内部服务器错误-这就是我尝试加载页面时得到的结果。@FrostyZ在对if语句的结构进行了一些修改后,它终于起作用了:)。谢谢您抽出时间!
$name = '';
$surName = '';
$email = '';

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

    $name = $_POST['name'];
    $surName = $_POST['surName'];
    $email = $_POST['email'];

    $errors = array();

    if ($name == '') { $errors[] = "Please type your name."; }
    if ($surName == '') { $errors[] = "Please type your surname."; }
    if (!checkEmail($email)) { $errors[] = "Wrong email format."; }

    if (count($errors) == 0) {
        // tip: use PDO or mysqli functions instead of mysql ones to bind variables.
        // currently there is a risk of SQL injection here
        mysql_query("INSERT INTO USR_INFO (NAME, MAIL, SURNAME) 
                     VALUES ('$name', '$email','$surName') ") or die(mysql_error());
        header('Location: thanks.php');
        exit;
    }
}

if (count($errors) > 0)
    echo '<p>Sorry, there are problems with the information you have provided:</p>';

foreach($errors as $error)
    echo '<p class="error">'.$error.'</p>';

echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">            
            <label for="name">First Name</label>
            <input type="text" name="name" id="name" value="'.htmlspecialchars($name).'" />
            <span class="required">&#42;</span>

            <label for="surName">Last Name</label>
            <input type="text" name="surName" id="surName" value="'.htmlspecialchars($surName).'" />
            <span class="required">&#42;</span>

            <label for="email">E-mail</label>
            <input type="email" id="email" name="email" placeholder="example@domain.com" value="'.htmlspecialchars($email).'" />
            <span class="required">&#42;</span>

            <input type="submit" name="submit" id="submit">
        </form>';