Problem getting the resulting JSON back to the calling contact form

2 replies · opened Jan 21, 2019

Ccesar_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 **successfully **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!

Ccesar_marreroJan 21, 2019

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

IsSMTP();
  $mail->Host = "smtp.dreamhost.com";
  $mail->SMTPDebug = 0;
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = 'tls';                            // -- TLS encryption, `ssl` 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 **HTML!**';
  
  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)    ? "**Name:**    $name
" : '';
          $email   = isset($email)   ? "**Email:**   $email
" : '';
          $phone   = isset($phone)   ? "**Phone:**   $phone
" : '';
          $service = isset($service) ? "**Service:** $service

" : '';
          $subject = isset($subject) ? "**Subject:** $subject
" : '';
          $message = isset($message) ? "**Message:** $message
" : '';
          
          $mail->Subject = '[Website Contact Form] RE: ' . strip_tags($service) . ' ';
          $mail->Body    = "

$name $email $phone $service $subject $message

 ";

          // -- 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 **all the fields** 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 **could not** be sent due to some unexpected error. Please try again later.  **Reason:** ' . $mail->ErrorInfo . '" }';
  endif; 
}
// -- Exception Handlers:
//
catch (Exception $e)
{
  echo $e->errorMessage();
}
catch (\Exception $e)
{
  echo $e->getMessage();
}
Ccesar_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