使用ajax&重定向页面jquery(php)

时间:2014-10-23 11:08:01

标签: javascript php jquery ajax

我想从页面a重定向到页面配置文件,并且在它们之间有一个帖子会话。在这种情况下,假设数据在字符串中是变量$name。到目前为止,我的代码在第a页上是这样的

            jQuery("#result").on("click",function(e){ 
            var $clicked = $(e.target);
            var $name = $clicked.find('.name').html();
            var decoded = $("<div/>").html($name).text();
            $('#searchid').val(decoded); 
            //the ajax script    
            $.ajax({
            type: 'POST',
            url: 'b.php',
            data: 'result='+$name,
            success: function() {
               window.location.href = "profile.php";  // replace
            }
            });      
            });  

并在page b上代码为:

<?php echo $_POST['result']?>  

结果应该是result中确定的值page a。 但是有一条错误消息说unidentified index。那我在哪里做错了?

2 个答案:

答案 0 :(得分:4)

可能是你的数据参数错了吗? 我有以下的ajax调用:

jQuery.ajax({
  type: "POST",
  url: "b.php",
  data: {
    result: $name
  },
  success: function() {
    window.location.href = "profile.php";  // replace
  }
});

答案 1 :(得分:0)

这是重定向后的新请求。为了访问结果,您需要在som类型的会话中进行sotre或再次传递它。

你可以像这样传递它,然后它将在$ _GET

success: function(data) {
  window.location.href = "profile.php?result="+data;  // replace
}