【问题标题】:PHP exceptions error messagesPHP 异常错误信息
【发布时间】:2017-01-02 09:52:55
【问题描述】:

我有一个 php 和 html 代码。我有 4 个用于发送电子邮件的输入(姓名、收件人、主题、消息),并且我有异常验证。现在我的编码工作:如果第一个输入为空,则显示错误消息,如果第二个输入为空(并且第一个不为空)显示错误消息,等等...如果我单击 SEND not one by,我想显示每条错误消息一个有 php 异常。有什么想法可以解决吗?

带有异常处理的PHP代码:

if (isset($_POST['Send'])) {
    class emailException extends Exception {}
    class email_validException extends Exception {}
    class subjectException extends Exception {}
    class messageException extends Exception {}

    $name = $_POST['name'];
    $to = $_POST['to'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    try {
        if($name == "") {
            throw new Exception("Töltsd ki a mezőt(1)!");
        }
        if($to == "") {
            throw new emailException("Töltsd ki a mezőt(2)!");
        }
        // Email cim ellenőrzése
        if (!filter_var($to, FILTER_VALIDATE_EMAIL)) {
            throw new email_validException("Hibás email cím!");
        }
        if($subject == "") {
            throw new subjectException("Töltsd ki a mezőt(3)!");
        }
        if($message == "") {
            throw new messageException("Töltsd ki a mezőt(4)!");
        }
        if ($name != "" && $to != "" && $subject != "") {
            echo "Email elküldve";
            $name = "";
            $to = "";
            $subject = "";
            $message = "";
        }
    }
    catch(emailException $e1) {
        $error_mail = $e1->getMessage();
    }
    catch(email_validException $e1) {
        $error_mail_valid = $e1->getMessage();
    }
    catch(subjectException $e2) {
        $error_subj = $e2->getMessage();
    }
    catch(messageException $e3) {
        $error_message = $e3->getMessage();
    }
    catch(Exception $e) {
        $error = $e->getMessage();
    }
}

HTML 输入表单:

<form name="myForm" onsubmit="//return validateForm();" method="post" action="feladat9.php">
    <p><input type="text" name="name" placeholder="Név" value="<?php echo $name; ?>"><label id="error_name"><?php echo $error ?></label></p>
    <p><input type="text" name="to" placeholder="Címzett" value="<?php echo $to; ?>"><label id="error_email"><?php
            echo "$error_mail $error_mail_valid";?></label></p>
    <p><input type="text" name="subject" placeholder="Tárgy" value="<?php echo $subject; ?>"><label id="error_subject"><?php echo $error_subj ?></label></p>
    <p><input type="text" name="message" placeholder="Üzenet" value="<?php echo $message; ?>"><label id="error_message"></label><?php echo $error_message ?></p>
    <input type="submit" value="Send" id="send" name="Send">
</form>

【问题讨论】:

  • 将异常消息保存在数组中而不是立即抛出,在完成条件检查后抛出所有异常
  • $name = isset($_POST['name']) ? $_POST['name'] : "" ; $to = isset($_POST['to']) ? $_POST['to'] : "" ; $subject = isset($_POST['subject']) ? $_POST['subject'] : "" ; $message = isset($_POST['message']) ? $_POST['message'] : "" ;
  • @Eugen 我应该在哪里插入这段代码?

标签: php html exception try-catch


【解决方案1】:

如果你想显示所有的错误,你不应该使用'throw new exception'的概念,因为异常停止下一个执行跳转到catch部分。

你可以这样试试:

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

    $name = $_POST['name'];
    $to = $_POST['to'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];

    $errors = array();

    if($name == "")
    {
         $errors['error'] = "Töltsd ki a mezőt(1)!";
    }
    if($to == "")
    {
        $errors['error_mail'] = "Töltsd ki a mezőt(2)!";
    }
    // Email cim ellenőrzése
    if (!filter_var($to, FILTER_VALIDATE_EMAIL))
    {
           $errors['error_mail_valid'] = "Hibás email cím!";
    }
    if($subject == "")
    {

        $errors['error_subj'] = "Töltsd ki a mezőt(3)!";
    }
    if($message == "")
    {
        $errors['error_message'] = "Töltsd ki a mezőt(4)!";
    }
    if ($name != "" && $to != "" && $subject != "")
    {
        echo "Email elküldve";

        $name = "";
        $to = "";
        $subject = "";
        $message = "";
    }

}

和 $errors 变量可用于显示错误。

【讨论】:

  • 我需要使用异常
  • 异常停止下一次执行到跳转到捕获部分。因此,如果空 $name 有异常,则其他代码执行将停止并跳转到 catch 部分。所以使用异常你不能一次显示所有错误。
  • 所以我不能同时删除所有错误消息?应该有一些解决方案
  • 你能解释一下为什么需要使用异常吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-18
  • 2014-03-15
  • 2015-04-19
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
相关资源
最近更新 更多