Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果使用ajax调用提交联系人表单,则标题(位置:/path)不起作用_Javascript_Php - Fatal编程技术网

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

Javascript 如果使用ajax调用提交联系人表单,则标题(位置:/path)不起作用,javascript,php,Javascript,Php,问题是,如果你试图要求提交我的联系方式然后标题(“位置:/path);开始不工作 if ($response->success) { $guest = mail($serverMail, $toAdmin, $mailMessageToServer, $headers); $server = mail($email, $toGuest, $mailMessageToGuest, $headers); if (($guest) && ($s

问题是,如果你试图要求提交我的联系方式<代码>然后标题(“位置:/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();
如果($response->success){
$guest=mail($serverMail,$toAdmin,$mailMessageToServer,$headers);
$server=mail($email、$toGuest、$mailMessageToGuest、$headers);
如果($guest)&($server)){
$data['status']=1;//为true
$data['message']=“您的成功消息”;//为true
}
} 
否则{
$data['status']=0;//为false
$data['message']=“您的错误消息”;//为false
}
//定义内容类型json如果您从未像echo“ss”那样进行回送,则无需执行此操作
标题('Content-Type:application/json');
echo json_encode($data);退出;
并获取ajax请求数据类型Json中的响应,以访问Json数组obj

$(文档).ready(函数(){
变量形式=$('mailformpro');
var响应=$('.status');
表格(‘提交’)功能(e){
e、 预防默认值();
$.ajax({
url:'contactform/ajax/contact.php',
键入:“POST”,
数据类型:“json”,
数据:form.serialize(),
beforeSend:function(){
响应。衰减();
response.fadeIn();
html('Loading…');
},
成功:功能(数据){
如果(data.status==1){
html(data.message.fadeIn();
window.location.href=“/contact/thankyou.html”;
}
否则{
//错误
html(data.message.fadeIn();
}
},
错误:函数(e){
控制台日志(e)
}
});
});
});

我不知道如何做到这一点?您不需要使用标题(“位置:/contact/thankyou.html”);在PHP代码中,因为您已经尝试在JavaScript中重定向到页面。您读过问题了吗?@Ropalimunsi,您甚至没有读过问题……是的,我读过,这就是为什么我建议您使用header()重新定位方法在使用AJAX时不起作用。您的PHP代码应该只从AJAX调用,而PHP主体内的重定向意味着您重定向该异步请求-而不是实际页面(您使用AJAX向服务器发出新请求,而不是使用您打开的页面)。您不需要
标题(“位置:…”);
在PHP内部,因为这是在Ajax的成功中完成的。如果在表单提交后重新定向,为什么还要使用Ajax呢?只需以正常方式提交表单,并在处理请求结束时执行标题重定向??感谢您的回答,但现在我甚至无法提交它。我想JS成功部分是不对的,但我会“你还没弄清楚哪里出了问题吗?”@parabellum如果jquery检查控制台出现问题,并在单击时使用alert()方法进行检查以检查提交事件是否正常工作not@parabellum请检查更新的php和js代码,如果有任何问题请告诉我thanks@parabellum我检查了代码工作正常,祝你好运。