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.

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

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵