【问题标题】:PHP Form validation using 'if'PHP表单验证使用'if'
【发布时间】:2017-01-15 13:59:33
【问题描述】:

我目前正在构建一个用于个人网站的非常小的“联系”表单。

表单有效,并且每个验证“if”语句单独有效,但是,例如,如果我输入了有效的电子邮件地址和电话号码但将消息留空,电子邮件仍会发送并且我会收到成功消息。

我的猜测是将小的“if”语句包含在一个检查我的必填字段是否不为空的语句中,尽管我不确定如何正确执行此操作,因为它将多个“if”嵌套到一个中。

干杯

<?php 
            // Validation goes here
            $errors = '';
            $success = 'Success! Your message has been sent. You should receive a reply within 48 hours.';

            $email = $_POST['email']; 
            $name = $_POST['thename']; 
            $comments = $_POST['comments'];
            $number = $_POST['number']; 

            if(empty($name) || empty($email) || empty($comments)) {
                $errors .= "Error: please input a name, email address and your message.";
            } else {
                $errors = '';
            }

            if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
                $errors .= "Error: Invalid email address";
            } else {
                $errors = '';
            }

            if (!preg_match("/^\(?0( *\d\)?){9,10}$/", $number)) {
                $errors .= "Error: Invalid phone number";
            } else {
                $errors = '';
            }

        ?>

        <!-- Display red error box or green success box depending on which is true -->
        <?php if(!empty($errors)): ?>
            <div class="validationbox | errorsbox">
                <?php echo $errors; ?>
            </div>
        <?php elseif(empty($errors)): ?>
            <div class="validationbox | successbox">
                <?php echo $success; ?>
            </div>
            <?php 
                $message = ''; // Blank message to start with so we can append to it.

                // Construct the message
                $message .= "
                    Name: {$_POST['thename']};
                    Email: {$_POST['email']};
                    Number: {$_POST['number']};
                    Enquiry-type: {$_POST['enquiry-options']};
                    Message: {$_POST['comments']};
";
                // test@testdomain.com
                $to = 'test-email-deleted-for-stackoverflow'; 
                $subject = 'Message from Portfolio';
                $from = $_POST['thename'];
                // YourSite@domain.com
                $fromEmail = 'test-email-deleted-for-stackoverflow';
                $header = 'From: ' . $from . '<' . $fromEmail . '>';
                mail($to,$subject,$message,$header);
            ?>
        <?php endif; ?>
    <?php endif; ?>

【问题讨论】:

  • 您需要提供包含发送或不发送电子邮件条件的代码。
  • 感谢@JBES,上面提供。
  • 我认为你必须删除你的 else 子句,因为每一次正确的检查都会删除所有之前设置的错误。
  • 所以把你的` if(empty($name) || empty($email) || empty($cmets)) { $errors .= "Error: please input a name, email地址和您的留言。”; } 其他 { $errors = ''; }` 阻止单个错误消息并摆脱 else 语句
  • 否则您可以将 $errors 用作数组并列出多个错误。 , 例如$errors[]= "Invalid email address" ... 然后你只需要检查 count($errors) >0 就知道表单是否无效

标签: php forms validation


【解决方案1】:

您的问题是,每次验证条件之一通过时,您都会将 $errors 重置回 ''

        if(empty($name) || empty($email) || empty($comments)) {
            $errors .= "Error: please input a name, email address and your message.";
        } else {
            $errors = '';
        }

        if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
            $errors .= "Error: Invalid email address";
        } else {
            $errors = '';
        }

        if (!preg_match("/^\(?0( *\d\)?){9,10}$/", $number)) {
            $errors .= "Error: Invalid phone number";
        } else {
            $errors = '';
        }

您不应该这样做,只需将错误消息留给以前的任何内容即可。这样,当你走到最后时,$errors 将包含一个包含所有错误消息组合的字符串。由于可能有多条消息,您可能希望在每条消息的末尾添加一个中断:

        if(empty($name) || empty($email) || empty($comments)) {
            $errors .= "Error: please input a name, email address and your message.<br>";
        } 

        if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
            $errors .= "Error: Invalid email address<br>";
        } 

        if (!preg_match("/^\(?0( *\d\)?){9,10}$/", $number)) {
            $errors .= "Error: Invalid phone number<br>";
        }

对于电子邮件,您可能希望仅在实际填写内容时才显示“无效的电子邮件地址”,因此您还可以在确定格式是否有效之前检查以确保其中有内容与否:

    if (!empty($email) && !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {

【讨论】:

  • 太棒了!也感谢您提供如此有用的解释:)
【解决方案2】:

根据提供的信息,我认为您应该像这样使用复杂的 if-elseif-else 语句:

`if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if this condition is true;
} else {
    code to be executed if all conditions are false;
} `

在您的特定情况下:

// Validation goes here
                $errors = '';
                $success = 'Success! Your message has been sent. You should receive a reply within 48 hours.';

                $email = $_POST['email']; 
                $name = $_POST['thename']; 
                $comments = $_POST['comments'];
                $number = $_POST['number']; 

                if(empty($name) || empty($email) || empty($comments)) {
                    $errors .= "Error: please input a name, email address and your message.";
                } elseif(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
                    $errors = 'Error:invalid email';
                }elseif(!preg_match("/^\(?0( *\d\)?){9,10}$/", $number){
                    $errors .= "Error: Invalid phone number";

                } else {
                    //Do this on successful validation comes here
                }

【讨论】:

  • 谢谢你,非常有帮助:)
  • Alex 我很高兴为您提供帮助。我在这个平台上得到了很多帮助。很高兴回报您。
【解决方案3】:

试试下面的代码对你有帮助。

<?php 
            // Validation goes here
            $errors = '';
            $success = 'Success! Your message has been sent. You should receive a reply within 48 hours.';

            $email = $_POST['email']; 
            $name = $_POST['thename']; 
            $comments = $_POST['comments'];
            $number = $_POST['number']; 

            if(empty($name) || empty($email) || empty($comments)) {
                $errors .= "Error: please input a name, email address and your message.";
            } else {
                   if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email)) {
                $errors .= "Error: Invalid email address";
            } else {
                $errors = '';
            }

            if (!preg_match("/^\(?0( *\d\)?){9,10}$/", $number)) {
                $errors .= "Error: Invalid phone number";
            } else {
                $errors = '';
            }
            }



        ?>

【讨论】:

  • 只有当你的email,cmets,name不为空时才会检查有效的email和phone。
  • 如果您的消息当时为空,则在您的代码中 $error 获取消息,但您的电子邮件地址是正确的,因此它进一步变为空白并发送消息
  • 如果您需要任何帮助,可以询问
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-02
  • 2020-06-22
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
  • 2018-06-30
  • 2011-02-17
相关资源
最近更新 更多