所以我有一个联系人字段,在提交时会发送一封电子邮件,我希望在完成时收到 javascript 警报但正在返回问题。
的JavaScript:
jQuery(document).ready(function() {
$.ajax({
url: './contactengine.php',
type: 'GET',
dataType: 'JSON',
success: function(response) {
alert("GOOD");
},
error: function() {
alert("BAD");
}
});
});
php:
<?php
$EmailFrom = "emailname@gmail.com";
$EmailTo = "emailto@gmail.com";
$Subject = "new contact from website";
$Name = Trim(stripslashes($_POST['Name']));
$company = Trim(stripslashes($_POST['company']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "company: ";
$Body .= $company;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success)
{
$result = array('success' => true);
}
else
{
$result = array('success' => false, 'message' => 'Something happened');
header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500);
}
echo json_encode($result);
?>
当我提交表单时,我被重定向到显示以下内容的页面:
{"success":true}
当我应该保持在同一页面上并且只是提醒良好时。