Iam facnigg和ajax问题,我在联系表单上工作,我遇到了ajax调用的问题, 即使字段都正确输入,调用也永远不会被取消。 我尝试了一些东西,但没有任何工作 Somene可以给我一个建议..?
非常感谢。
$.ajax({
url: "assets/contact_form/process-contact.php",
type: "POST",
dataType: "html",
data: userData,
beforeSend: function () {
$( sb ).html('Sending...');
},
success: function (response) {
if (response == true) {
setTimeout(function () {
window.location = 'index.html';
}, 1000);
} else {
$( sb ).html('Can not send, please try latter'); //IT GOES STRIGHT TO HERE
}
}
});
}
});
这是php代码:
<?php
$userData['name'] = filter_var(trim($_POST['name']),
FILTER_SANITIZE_STRING);
$userData['email'] = filter_var(trim($_POST['email']),
FILTER_VALIDATE_EMAIL);
$userData['phone'] = filter_var(trim($_POST['phone']),
FILTER_SANITIZE_STRING);
$userData['message'] = filter_var(trim($_POST['message']),
FILTER_SANITIZE_STRING);
if( ! in_array( null, $userData ) ){
$my_email = 'roni.itzhaik@gmail.com';
$nameRegx = "/^[' a-zא-ת]+(\s[' a-zא-ת]+)*$/i";
$phoneRegx = "/^(?:0(?!(5|7))(?:2|3|4|8|9))(?:-?\d){7}$|^(0(?=5|7)(?:-?\d)
{9})$/";
if( strlen($userData['name']) > 2 && strlen($userData['name']) < 70 &&
preg_match($nameRegx, $userData['name']) ) {
if( preg_match($phoneRegx, $userData['phone']) ){
if( strlen($userData['message']) > 2 ){
$subject = 'Message from ' . $userData['name'] ;
$message_phone= $userData['message'].'Phone number:
'.$userData['phone'];
$headers = 'From: ' . $userData['email']. "\r\n";
mail($my_email, $subject, $message_phone, $headers);
// send mail
// Save data to db (DON'T FORGET TO CLEAN AGAINST SQL INJECTIONS!!!)
echo true;
}}}}
答案 0 :(得分:0)
这可能有效。
......
if (response) {
setTimeout(function () {
window.location = 'index.html';
.....
答案 1 :(得分:0)
如果你可以返回一个对象会更好:
echo json_encode(['success' => true]);
然后检查您的成功回调将如下所示:
if (response.success){
//Your logic
}
希望这有帮助。