Javascript ajax jquery表单已停止发送电子邮件
我有一个表单,以前从服务器发送电子邮件,但突然它停止了工作。我似乎想不出这个问题。当我使用没有ajax的表单时,电子邮件就可以完美地工作。我在开发工具中看到了HTTP200代码,没有错误。任何帮助都将不胜感激。多谢各位 html:Javascript ajax jquery表单已停止发送电子邮件,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个表单,以前从服务器发送电子邮件,但突然它停止了工作。我似乎想不出这个问题。当我使用没有ajax的表单时,电子邮件就可以完美地工作。我在开发工具中看到了HTTP200代码,没有错误。任何帮助都将不胜感激。多谢各位 html: 提交 收到消息。 函数重置_form(){ $(“#form1”)[0]。重置(); } 功能重置成功消息(){ $('.success_message').addClass('hide'); } $(文档).ready(函数(){ $('#name')。单击(函数
提交
收到消息。
函数重置_form(){
$(“#form1”)[0]。重置();
}
功能重置成功消息(){
$('.success_message').addClass('hide');
}
$(文档).ready(函数(){
$('#name')。单击(函数(){
$('.success_message').addClass('hide');
});
$('.email')。单击(函数(){
$('.success_message').addClass('hide');
});
$('.company')。单击(函数(){
$('.success_message').addClass('hide');
});
$(“#消息”)。单击(函数(){
$('.success_message').addClass('hide');
});
$('#form1')。提交(函数(e){
$('.success_message').addClass('hide');
e、 预防默认值();
$.ajax({
url:'serverside.php',
键入:“post”,
数据:$('#form1')。序列化(),
成功:功能(响应){
如果(响应==“已提交”){
重置_形式();
$('.success_message').removeClass('hide');
}
}
});
});
});
serverside.php
<?php
$email_to = 'x@domain.com';
$email_subject = 'form submission';
$email = $_POST ['email'];
$required = array('name','email','company', 'message');
$form1_complete = FALSE;
$validation = array();
if(!empty($_POST)) {
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
foreach($required as $field) {
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
if($_POST[$field] == '') array_push($validation, $field);
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
if(count($validation) == 0) {
$email_content = 'New Comment: ' . "\n\n";
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n\n ";
}
$recaptcha_secret = "x";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
$response = json_decode($response, true);
if($response["success"] === true) {
mail($email_to, $email_subject, $email_content, "From:" . $email);
}
else
{
}
echo 'submitted';
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
您似乎遗漏了Recaptcha的秘密。如果状况良好,则移除Recaptcha
希望有帮助:)您做了哪些调试?尝试检查
mail()
函数的结果。检查邮件服务器日志。@Barmar非常感谢您的回复。我刚刚检查了一下,发现我没有访问该日志的权限。我会问主机提供商。事实上,我甚至不知道有日志文件,谢谢你提供的信息。:)就是这样:)我不知道出了什么问题,也从来没有想过验证码,我只是用你做的做了测试,效果非常好。非常感谢你!!!!是否有办法添加工作if条件?当我在没有ajax的情况下使用表单时,邮件会按照我现在具备的条件发送。再次感谢你,我快疯了。:)@没问题!很乐意帮忙。是的,您可以在不使用ajax的情况下发送Recaptcha。只需在formaction属性中添加php文件路径,其余的将按原样工作。$_POST['g-recaptcha-response']将获得响应,但首先必须让recaptcha工作。为此,您必须从这里添加recaptcha密钥:登录后,您将获得一个密钥,您必须将其放置在data sitekey中以代替x:并将其放置在php代码中:$recaptcha_secret=“x”;雷帕查正在工作。我在主服务器文件中都有验证码密钥和密码。在这里我只放了“x”。我想有验证码验证服务器端的秘密了。此时,“如果条件”不起作用。如果没有那封信,邮件就会被发送@M工作正常时,我插入了recaptcha密钥,并且能够在服务器端接收响应$recaptcha_secret=“6LeIxAcTAAAAAGG-vfi1tnrwxmznfuoj4wifjwe”$response=file_get_contents(“$recaptcha_secret.”&response=“.$”POST['g-recaptcha-response'])$response=json_decode($response,true);我在hostgator和bluehost中测试了相同的代码。他们都不发送电子邮件。当我像你说的那样拿出“如果条件验证码”,我会收到来自两个主机的电子邮件。是否有其他方法来编写该条件?谢谢你的时间和帮助:)@Mr
<?php
$email_to = 'x@domain.com';
$email_subject = 'form submission';
$email = $_POST ['email'];
$required = array('name','email','company', 'message');
$form1_complete = FALSE;
$validation = array();
if(!empty($_POST)) {
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
foreach($required as $field) {
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
if($_POST[$field] == '') array_push($validation, $field);
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
if(count($validation) == 0) {
$email_content = 'New Comment: ' . "\n\n";
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n\n ";
}
$recaptcha_secret = "x";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
$response = json_decode($response, true);
if($response["success"] === true) {
mail($email_to, $email_subject, $email_content, "From:" . $email);
}
else
{
}
echo 'submitted';
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
<?php
$email_to = 'ahmed_rises@hotmail.com'; //-----------> Invalid Email Id was added here
$email_subject = 'form submission';
$email = $_POST ['email'];
$required = array('name','email','company', 'message');
$form1_complete = FALSE;
$validation = array();
if(!empty($_POST)) {
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
foreach($required as $field) {
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
if($_POST[$field] == '') array_push($validation, $field);
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
if(count($validation) == 0) {
$email_content = 'New Comment: ' . "\n\n";
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n\n ";
}
//Recaptca Secrets are Missing?????? Random string passed!
$recaptcha_secret = "x";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret="
.$recaptcha_secret."&response=".$_POST['g-recaptcha-response']); //-----> Also Recapta response from
//the form is also missing since there its not working and neither getting passed
$response = json_decode($response, true);
//Printed the Output in which it shows the recapta Error
echo "<pre>";
print_r($response);
//If you ae to remove the Recapta Condition, the mail will be send!
// if($response["success"] === true) {
echo "==========";
echo "email_subject:".$email_subject.", email:".$email.",email_to:".$email_to;
mail($email_to, $email_subject, $email_content, "From:" . $email);
echo "==========";
// }
// else
// {
// echo "Failed";
// }
echo "<br>";
echo 'submitted';
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>