donglun4682 2019-02-19 05:46
浏览 71
已采纳

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

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. 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. 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. Even if I do not forcefully exit the script with die or 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.

Any help will be greatly appreciated.

I spent hours searching the web for answers for no avail. I tried to place the json_encode function as different location in the file, and it did not work. 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. However that is not happening.

  • 写回答

1条回答 默认 最新

  • doulao7998636570 2019-02-21 11:46
    关注

    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. I had to change the conditional if statement to use array_filters instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改