2019-02-19 05:46
浏览 71


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 );
} else {  

 $mail = new PHPMailer( true );

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

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

    /* 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条回答 默认 最新

相关推荐 更多相似问题