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.

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

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上