简体   繁体   English

如何在 json_encode 输出后执行其余的 php 脚本?

[英]How to execute the rest of the php script after json_encode output?

I have a simple form with four input fields, name, email, phone, and message.我有一个简单的表单,其中包含四个输入字段:姓名、电子邮件、电话和消息。 I am using JavaScript with Ajax to post my input fields to a PHP file for validations and mailing via phpmailer.我正在使用 JavaScript 和 Ajax 将我的输入字段发布到 PHP 文件,以便通过 phpmailer 进行验证和邮寄。

I collect the errors in an array from the validation functions.我从验证函数中收集数组中的错误。 The I use and if statement to send the errors back to the html page via json_encode(array) function and exit the script. I use and if 语句通过json_encode(array)函数将错误发送回 html 页面并退出脚本。 Else, I let the script to continue to and successfully send the email to my inbox.否则,我让脚本继续并成功地将电子邮件发送到我的收件箱。

The problem I have is json_encode(array) exit the script where errors were found or not.json_encode(array)的问题是json_encode(array)退出发现错误的脚本。 Even if I do not forcefully exit the script with die or exit.即使我没有用 die 或 exit 强制退出脚本。 In that respect, no email is sent as the script is prematurely terminated.在这方面,由于脚本过早终止,因此不会发送电子邮件。
I listed below a few lines from my php file to show how my if statement is set up.我在我的 php 文件下面列出了几行,以显示我的 if 语句是如何设置的。

I have searched the web for answers to no avail.我已经在网上搜索了无济于事的答案。 I tried to place the json_encode function as different location in the file, and it did not work.我试图将json_encode函数放在文件中的不同位置,但没有奏效。 I tried combining the error array with the success or failure strings from the mail sent function and did not work.我尝试将错误数组与邮件发送函数中的成功或失败字符串结合起来,但没有奏效。

if ( $errors != null ) {
    echo json_encode( $errors );
    exit;
} else {  

 $mail = new PHPMailer( true );

    /* Set the mail sender. */
    $mail->setFrom( 'jsmith@gmail.com', 'John Smith' );

    /* Add a recipient. */
    //set who is receving mail
    $mail->addAddress( 'heh@hotmail.com' );

    /* Set the subject. */
    $mail->Subject = 'New message from contact form';

    /* Set email to be sent as HTML */
    $mail->isHTML( true );

    /* Set the mail message body. */
    $mail->Body =

I am expecting that once the if statement clears the errors, the entire php file script will continue until the data is emailed.我期待一旦 if 语句清除了错误,整个 php 文件脚本将继续,直到通过电子邮件发送数据。 However that is not happening.然而,这并没有发生。

I found the answer from another related question I posted on a different post.我在另一个帖子上发布的另一个相关问题中找到了答案。 The errors array was containing all null variables that the conditional statement I had to determine whether the mail should be sent on Boolean false (no errors found) was not considering the array as empty.错误数组包含所有空变量,我必须确定是否应在布尔值为 false(未发现错误)时发送邮件的条件语句并未将数组视为空变量。 I had to change the conditional if statement to use array_filters instead.我不得不更改条件 if 语句以使用 array_filters。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM