Problem getting the resulting JSON back to the calling contact form

2 replies·opened Jan 21, 2019
C
cesar_marreroJan 21, 2019

The contact form (modal) is able to send email, but the resulting JSON being returned by the PHP code (via echo commands) just creates a HTML page with the following message:

{ "alert": "success", "message": "We have <strong>successfully </strong>received your message and will get back to you as soon as possible." }

Obviously, we would prefer the message go back to the modal form, but instead we get a separate page with the above JSON. I'm including the custom PHP code used in place of sendemail.php along with the HTML.

Any help is greatly appreciated!

C
cesar_marreroJan 21, 2019

Your system did not allow the PHP code to be included as an attachement, so here's the code:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$submit_type      = php_sapi_name();
$submit_status    = false;
$message_success  = 'We have <strong>successfully</strong> received your message and will get back to you as soon as possible.';
$recaptcha_secret = '6LeXboYUAAAAAIdmpTv2PTL9GxlQfYgktjvzCU9D';// -- Add this only if you use reCaptcha with your Contact Forms

if ($submit_type == "cli") {
  require 'D:\xampp\vendor\autoload.php'; // -- This is only needed when run on the console
}
      
require 'phpmailer/src/Exception.php';
require 'phpmailer/src/PHPMailer.php';
require 'phpmailer/src/SMTP.php';

$mail = new PHPMailer(TRUE);  // -- Passing TRUE to the constructor enables exceptions.

try {
  $mail->IsSMTP();
  $mail->Host = "smtp.dreamhost.com";
  $mail->SMTPDebug = 0;
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = 'tls';                            // -- TLS encryption, <code>ssl</code> also accepted
  $mail->Port = 587;                                    // -- TCP port
  $mail->Username = "dispatch@cahins.work";
  $mail->Password = "hdNCWua-";
  $mail->setFrom('dispatch@cahins.work', 'Dispatch');
  $mail->addAddress('developer@xentient.technology', 'Developer');
  $mail->Subject = 'TEST';
  $mail->isHTML(true);
  $mail->Body = 'Testing messages using <b>HTML!</b>';
  
  switch ($submit_type) {
    case "cli":
      $submit_status = $mail->Send();
      break;
      
    case "cgi-fcgi":
    case "apache2handler":
      if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
        if( $_POST['template-contactform-email'] != '' ) {

          // -- Pull all of the email data from the POST
          //
          $name    = isset( $_POST['template-contactform-name'] )    ? $_POST['template-contactform-name'] : '';    
          $email   = isset( $_POST['template-contactform-email'] )   ? $_POST['template-contactform-email'] : '';   
          $phone   = isset( $_POST['template-contactform-phone'] )   ? $_POST['template-contactform-phone'] : '';   
          $service = isset( $_POST['template-contactform-service'] ) ? $_POST['template-contactform-service'] : ''; 
          $subject = isset( $_POST['template-contactform-subject'] ) ? $_POST['template-contactform-subject'] : ''; 
          $message = isset( $_POST['template-contactform-message'] ) ? $_POST['template-contactform-message'] : ''; 
          
          $mail->SetFrom( $email , $name );
          $mail->AddReplyTo( $email , $name );
          
          // -- [CAM 2018-12-19] Route the email to the proper recipient, depending on the service selected:
          //
          // switch ($service) {
            // case "Business":
              // $mail->AddAddress( 'scrawford@cahins.com' , 'Stacey Crawford' );
              // break;
            // case "Benefits":
              // $mail->AddAddress( 'tjiles@cahins.com' , 'Tiffany Jiles' );
              // break;
            // case "Claim":
              // $mail->AddAddress( 'claims@cahins.com' , 'Claims Dept' );
              // break;
            // case "Certification":
              // $mail->AddAddress( 'cert@cahins.com' , 'Certifications' );
              // break;
            // default:
              // $mail->AddAddress( 'cknotts@cahins.com' , 'Christopher Knotts' );
              // break;
          // }
          
          // -- Compose the HTML message
          //
          $name    = isset($name)    ? "<strong>Name:</strong>    $name
" : '';
          $email   = isset($email)   ? "<strong>Email:</strong>   $email
" : '';
          $phone   = isset($phone)   ? "<strong>Phone:</strong>   $phone
" : '';
          $service = isset($service) ? "<strong>Service:</strong> $service

" : '';
          $subject = isset($subject) ? "<strong>Subject:</strong> $subject
" : '';
          $message = isset($message) ? "<strong>Message:</strong> $message
" : '';
          
          $mail->Subject = '[Website Contact Form] RE: ' . strip_tags($service) . ' ';
          $mail->Body    = "<pre> $name $email $phone $service $subject $message</pre> ";

          // -- Runs only when reCaptcha is present in the Contact Form
          //
          if( isset( $_POST['g-recaptcha-response'] ) ) {
            $recaptcha_response = $_POST['g-recaptcha-response'];
            $response = file_get_contents( "https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response );
    
            $g_response = json_decode( $response );
    
            if ( $g_response->success !== true ) {
              echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
              die;
            }
          }
    
          // -- Uncomment the following Lines of Code if you want to Force reCaptcha Validation
          //
          if( !isset( $_POST['g-recaptcha-response'] ) ) {
            echo '{ "alert": "error", "message": "Captcha not Submitted! Please Try Again." }';
            die;
          }

          // == Send the email
          //
          $submit_status = $mail->Send();
        } 
        else {
          echo '{ "alert": "error", "message": "Please fill up <strong>all the fields</strong> and try again." }';
        }
      }      
      break;
      
    default:
      echo 'Unknown Submission type
';
      break;
  }
  
  // -- Send an acknowledgement message if successful, otherwise send an error message
  //
  if($submit_status):
    echo '{ "alert": "success", "message": "' . $message_success . '" }';
  else:
    echo '{ "alert": "error", "message": "Email <strong>could not</strong> be sent due to some unexpected error. Please try again later.  <strong>Reason:</strong> ' . $mail->ErrorInfo . '" }';
  endif; 
}
// -- Exception Handlers:
//
catch (Exception $e)
{
  echo $e->errorMessage();
}
catch (\Exception $e)
{
  echo $e->getMessage();
}
C
cesar_marreroJan 21, 2019

Disregard ... I found the problem in the HTML file where the response pop-up was commented-out.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard