繁体   English   中英

如果使用ajax调用联系表单提交,标题(位置:/路径)不起作用

[英]if use ajax call for contact form submitting, header(location:/path) not working

问题是如果尝试通过ajax调用提交我的联系表格。 then header("location: /path);开始不起作用。

if ($response->success) {
      $guest = mail($serverMail, $toAdmin, $mailMessageToServer, $headers);
      $server = mail($email, $toGuest, $mailMessageToGuest, $headers);
      if (($guest) && ($server)) {
        // header("location: /contact/thankyou.html");
      } 
  } else {
    echo "Invalid captcha! Please enter again. ";
  }

是的,因为标头重定向不起作用。 我把它注释掉。 并尝试在 ajax 调用中重定向页面,如下所示。

$(document).ready(function() {
    var form = $('#mailformpro');
    var response = $('.status');
    form.on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            url: 'contactform/ajax/contact.php',
            type: 'POST',
            dataType: 'html',
            data: form.serialize(),
            beforeSend: function(){
                response.fadeOut();
                response.fadeIn();
                response.html('Loading...');
            },
            success: function(data){
                response.html(data).fadeIn();
                window.location.href ="/contact/thankyou.html";
            },
            error: function(e){
                console.log(e)
            }
        });
    });
    });

但这一次,它只是在.status div 内重定向! 就像在图像中通常我在那个 div 中显示一条错误消息......

您必须将标头作为 Base64 字符串发送,然后在服务/后端中对其进行解码。

嘿,这是错误的做法,如果您使用 ajax req 然后 php 无法重定向到您的目的地所有您需要在 ajax 成功响应中重定向简单我们

$data = array();
if ($response->success) {
    $guest = mail($serverMail, $toAdmin, $mailMessageToServer, $headers);
    $server = mail($email, $toGuest, $mailMessageToGuest, $headers);
    if (($guest) && ($server)) {
        $data['status'] = 1; // for true
        $data['message'] = "your success message"; // for true
    }
} 
else {
    $data['status'] = 0; // for false
    $data['message'] = "your error message"; // for false
}
//define content type json if you never echo like echo 'ss' then no need to this
header('Content-Type: application/json');
echo json_encode($data);exit;

并在 ajax 请求 dataType Json 中获取响应以访问 json 数组 obj

$(document).ready(function() {
  var form = $('#mailformpro');
  var response = $('.status');
  form.on('submit', function(e) {
    e.preventDefault();
    $.ajax({
      url: 'contactform/ajax/contact.php',
      type: 'POST',
      dataType: 'json',
      data: form.serialize(),
      beforeSend: function() {
        response.fadeOut();
        response.fadeIn();
        response.html('Loading...');
      },
      success: function(data) {
        if (data.status == 1) {
        response.html(data.message).fadeIn();
        window.location.href = "/contact/thankyou.html";
        }
        else{
        // for error
        response.html(data.message).fadeIn();
        }
      },
      error: function(e) {
        console.log(e)
      }
    });
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM