I have a problem with an ajax call to php mail() script failing on success, no json data is returned. It's basically just posting a form to a php script, validating & returning some json.
If I encounter either a validation error the json is returned correctly & my jquery executes as expected.
If NO errors are encountered, PHP processes and sends mail correctly but no data is returned to my jquery script.
Here is my code:
<?php
require "gump.class.php";
$gump = new GUMP();
$mailto = 'me@mydomain.com';
$subject = 'A new form inquiry has been submitted.';
$output = array(
'status' => 'success',
'msg' => 'Mail processed successfully',
'success' => 'success',
);
function render_email($input) {
//error_log("render_email " . print_r($input,TRUE), 0);
ob_start();
include "email-template.phtml";
return ob_get_contents();
}
$input = $gump->sanitize($_POST);
$gump->validation_rules(array(
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required',
//'country' => 'required|valid_email',
//'gender' => 'required|exact_len,1',
//'company' => 'required|valid_cc|max_len,2|min_len,1',
//'bio' => 'required'
));
$gump->filter_rules(array(
'first_name' => 'trim|sanitize_string',
'last_name' => 'trim|sanitize_string',
'email' => 'trim|sanitize_string',
));
$validated = $gump->run($_POST);
if($validated === false){
error_log("GUMP: validation Error: " . print_r($gump->get_readable_errors(true),TRUE));
$output = array(
'status' => 'error',
'msg' => '<strong>Validation Error: </strong>' . $gump->get_readable_errors(true),
'error' => 'error',
);
}else{
error_log("GUMP: Successful Validation, processing mail",0);
// ghead & mail the form
$to = $mailto ;
$subject = $subject;
$body = render_email($input);
$headers = "From: Metz Tea <sales@mydomain.com>" . "
";
$headers .= "Reply-To: sales@mydomain.com
";
$headers .= "Return-Path: info@example.com
";
$headers .= "X-Mailer: PHP
";
$headers .= "MIME-Version: 1.0" . "
";
$headers .= "Content-type:text/html;charset=UTF-8" . "
";
if(!mail($to,$subject,$body,$headers)){
$output = array(
'status' => 'error',
'msg' => 'The system failed to send mail.',
'error' => 'error',
);
};
error_log("mail complete",0);
}
header("HTTP/1.1 200 OK");
header('Content-type: application/json');
$output = json_encode($output);
echo $output;
return;
and the jquery:
$('form').submit(function(event){
event.preventDefault();
})
$(document).on("forminvalid.zf.abide", function(event,frm) {
var modal = $('form').closest('.reveal').attr('id');
$(".success").hide();
$(".alert").show();
console.log("Form id "+event.target.id+" is invalid");
formToTop(modal);
}).on("formvalid.zf.abide", function(event,frm) {
console.log("Form id "+event.target.id+" is VALID");
var modal = $('form').closest('.reveal').attr('id');
var data = frm.serializeArray();
$.ajax({
type : 'POST',
url : 'process.php',
data : data,
dataType : 'json',
encode : true
}).done(function(data) {
console.log('completed successfully '+ data);
if (data.status != 'success') {
console.log('AJAX returned error = ' + data.msg);
$('.callout p').html(data.msg);
$('.alert').show();
formToTop(modal);
} else {
console.log( 'AJAX returned success = ' + data.status);
$('.callout p').html(data.msg);
$('#orderform').find("input[type=text], textarea, select").val("");
$('.alert').hide();
$('.success').show();
$('form').slideUp('500');
formToTop(modal);
}
}).fail(function(data) {
//error
});
event.preventDefault();
});
It must be the mail() function somehow, it does send mail on success, but no data is sent back to the ajax script.
what is my error here?