如何跳过PHP错误并呈现HTML的其余部分?

如何跳过PHP错误并呈现HTML的其余部分?,php,html,Php,Html,我用一个表单创建了一个联系人页面,问题是如果我发送了电子邮件,并且错误是由PHPmailer引起的,那么页面的其余部分不会呈现,但是如果是验证错误,那么整个页面就会呈现出应有的效果 不幸的是,我是在本地主机上运行的,所以我知道在发送时会有连接错误,但是在发现错误后,我如何才能使页面继续呈现 包含PHP文件的HTML: include_once('validate.php'); 除了包含的PHP文件之外,我知道将出现错误连接: <?php $name = $srn = $

我用一个表单创建了一个联系人页面,问题是如果我发送了电子邮件,并且错误是由PHPmailer引起的,那么页面的其余部分不会呈现,但是如果是验证错误,那么整个页面就会呈现出应有的效果

不幸的是,我是在本地主机上运行的,所以我知道在发送时会有连接错误,但是在发现错误后,我如何才能使页面继续呈现

包含PHP文件的HTML:

include_once('validate.php');
除了包含的PHP文件之外,我知道将出现错误连接:

<?php
         $name = $srn = $email = $txt = "";
         $border1 = $border2 = $border3 = $border4 = "";
         $errornum1 = $errornum2 = $errornum3 = $errornum4 = 0;

         $errorname = $errorsrn = $errormail =  $errortxt =  $secondcap = "";

          function test_input($data) {
              $data = trim($data);
              $data = stripslashes($data);
              $data = htmlspecialchars($data);
              $data = preg_replace('/\s+/',' ', $data);
              return $data;
        }

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

        if($_POST['hidden'] !== ""){
                $secondcap ='<span style="text-align:center;background-color:RGBA(255, 255, 255, 0.7);position:absolute;left:0px;width:100%;color:#f00;font-size:1.2em;">There Appears to be an issue, Please try again</span>';

        }else{
                $secondcap = "";

            if($_POST['name'] == ""){
                $errorname = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Name Field is Empty</span>';
                $name = "";
                $border1 = 'style="border:#f00 solid 1px;"';
                $errornum1 = 1;
            }else{
                if(!preg_match("/^[a-zA-Z ]*$/",$_POST['name'])){
                    $errorname = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Name Field contains invalid characters</span>';
                    $name = "";
                    $border1 = 'style="border:#f00 solid 1px;"';
                    $errornum1 = 1;
                }else{
                    $errorname = "";
                    $name = test_input($_POST['name']);
                    $border1 = 'style="border:#2f2 solid 1px;"';
                    $errornum1 = 0;
                }
            }

            if($_POST['srn'] == ""){
                $errorsrn = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Surname Field is Empty</span>';
                $srn = "";
                $border2 = 'style="border:#f00 solid 1px;"';
                $errornum2 = 1;
            }else{

                if(!preg_match("/^[a-zA-Z ]*$/",$_POST['srn'])){
                    $errorsrn = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Surname Field contains invalid characters</span>';
                    $srn = "";
                    $border2 = 'style="border:#f00 solid 1px;"';
                    $errornum2 = 1;
                }else{
                    $errorsrn = "";
                    $srn = test_input($_POST['srn']);
                    $border2 = 'style="border:#2f2 solid 1px;"';
                    $errornum2 = 0;
                }
            }

            if($_POST['email'] == ""){
                $errormail = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Email Field is Empty</span>';
                $email = "";
                $border3 = 'style="border:#f00 solid 1px;"';
                $errornum3 = 1;
            }else{
                if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
                    $errormail = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Email Field Requires an "@Service" and ".Web" E.G thisemail@gmail.com</span>';
                    $email = "";
                    $border3 = 'style="border:#f00 solid 1px;"';
                    $errornum3 = 1;
                }else{
                    $errormail = "";
                    $email= test_input($_POST['email']);
                    $border3 = 'style="border:#2f2 solid 1px;"';
                    $errornum3 = 0;
                }
            }

            if($_POST['text'] == ""){
                $errortxt = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Message is Empty</span>';
                $txt= test_input($_POST['text']);
                $border4 = 'style="border:#f00 solid 1px;"';
                $errornum4 = 1;
            }else{
                if(!preg_match("/^[a-zA-Z ]*$/",$_POST['text'])){
                    $errortxt = '<span style="color:#f00;font-weight:bold;font-size:0.9em;padding:0px 10px;">*Message field Contains Invalid characters</span>';
                    $txt= "";
                    $border4 = 'style="border:#f00 solid 1px;"';
                    $errornum4 = 1;
                }else{
                    $errortxt = "";
                    $txt= test_input($_POST['text']);
                    $border4 = 'style="border:#2f2 solid 1px;"';
                    $errornum4 = 0;
                }
            }

            if($errornum1 + $errornum2 + $errornum3 + $errornum4 == 0){
                require("../PHPMailer-5.2-stable/PHPMailerAutoload.php");
                require '../PHPMailer-5.2-stable/class.phpmailer.php';

                $mail = new PHPMailer();

                // set mailer to use SMTP
                $mail->IsSMTP();
                $mail->SMTPDebug = 0;

                // As this email.php script lives on the same server as our email server
                // we are setting the HOST to localhost
                $mail->Host = "smtp.gmail.com";  // specify main and backup server

                $mail->SMTPAuth = true;     // turn on SMTP authentication
                $mail->SMTPSecure = "tls";
                $mail->Port = 465;
                $mail->Username = "****";  // SMTP username
                $mail->Password = "***"; // SMTP password

                // $email is the user's email address the specified
                // on our contact us page. We set this variable at
                // the top of this page with:
                // $email = $_REQUEST['email'] ;
                $mail->From = $email;

                // below we want to set the email address we will be sending our email to.
                $mail->AddAddress("****", "****");

                // set word wrap to 50 characters
                $mail->WordWrap = 50;
                // set email format to HTML
                $mail->IsHTML(true);

                $mail->Subject = "You have received feedback from your website!";
                $mail->Body    = $txt;
                $mail->AltBody = $txt;

                if(!$mail->Send()){
                    $secondcap = '<h3 style="color:#f00;text-align:center;">There Was an error sending your email please try again</h3>';
                    return;
                }else{
                    $secondcap = '<h3 style="color:#0f0">Your Email has been sent</h3>';
                    return;
                }
            }
        }
    }

?>

<htmL>
    <form method="post" action="contact.php#head" autocomplete="off">

        <h2>Form</h2> 

        <?php echo $secondcap; ?>

        <div class="form">
            <label>Name<?php echo $errorname; ?></label><input type="text" <?php echo $border1; ?> name="name" value="<?php echo $name ?>"><br>
            <label>Surname <?php echo $errorsrn; ?></label><input type="text" <?php echo $border2; ?> name="srn" value="<?php echo $srn ?>"> <br>
            <label>Email <?php echo $errormail; ?></label><input type="email" <?php echo $border3; ?> name="email" value="<?php echo $email ?>"><br>
            <label style="display:none;">Hidden</label><input type="text" name="hidden" style="display:none">
        </div>

        <div class="form line">
            <label>Message <?php echo $errortxt; ?></label><textarea name="text" <?php echo $border4; ?> ><?php echo $txt ?></textarea>
        </div>


        <div class="form">
            <!--    <h3>Please, complete puzzle to submit</h3> -->
            <input type="submit" name="submit" value="Send">
        </div>
    </form>
</html>

如果可能出现异常,请尝试以下操作:

try {

    if(!$mail->Send()){
        $secondcap = '<h3 style="color:#f00;text-align:center;">There Was an error sending your email please try again</h3>';
        return;
    }else{
        $secondcap = '<h3 style="color:#0f0">Your Email has been sent</h3>';
        return;
    }

} catch (Exception $e) {
    $secondcap = '<h3 style="color:#0f0">Your Email has been sent</h3>';
    echo $e->getMessage();
}
试试看{
如果(!$mail->Send()){
$secondcap='发送电子邮件时出错,请重试';
返回;
}否则{
$secondcap='您的电子邮件已发送';
返回;
}
}捕获(例外$e){
$secondcap='您的电子邮件已发送';
echo$e->getMessage();
}

一些try-catch块怎么样?RTM:为什么在本地主机上运行应用程序时会出现问题?你为什么不把这些修好?
try {

    if(!$mail->Send()){
        $secondcap = '<h3 style="color:#f00;text-align:center;">There Was an error sending your email please try again</h3>';
        return;
    }else{
        $secondcap = '<h3 style="color:#0f0">Your Email has been sent</h3>';
        return;
    }

} catch (Exception $e) {
    $secondcap = '<h3 style="color:#0f0">Your Email has been sent</h3>';
    echo $e->getMessage();
}