最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - redirect page using ajax & jquery (php) - Stack Overflow

programmeradmin2浏览0评论

i want to redirect from page a to page profile and in between there is a post session on them. in this case let's say the data is variable $name in string. so far my code is like this on page 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
            }
            });      
            });  

and on page b the code is:

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

the oute should be the value from result in which determined on page a. but so there is an error message saying unidentified index. so where am i doing wrong?

i want to redirect from page a to page profile and in between there is a post session on them. in this case let's say the data is variable $name in string. so far my code is like this on page 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
            }
            });      
            });  

and on page b the code is:

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

the oute should be the value from result in which determined on page a. but so there is an error message saying unidentified index. so where am i doing wrong?

Share Improve this question edited Oct 23, 2014 at 11:30 Arun 3,7419 gold badges50 silver badges90 bronze badges asked Oct 23, 2014 at 11:08 user2891092user2891092 1631 gold badge2 silver badges10 bronze badges 5
  • 1 on which line it shows undefined index ? – Arun Commented Oct 23, 2014 at 11:14
  • How does the html look? – Albin Commented Oct 23, 2014 at 11:15
  • I tried this and it works fine for me. – Albin Commented Oct 23, 2014 at 11:22
  • @Albin the error occured when i call the data on page profile. it is actually an autoplete search and when i click on the name, it directs me into the profile page. the above code is the click function. – user2891092 Commented Oct 23, 2014 at 11:32
  • @Arun it shows on the profile page. it seems does not recognize the $_POST['result'] data – user2891092 Commented Oct 23, 2014 at 11:32
Add a ment  | 

2 Answers 2

Reset to default 4

Could it be, that your data parameter is wrong? I have my ajax calls as folowing:

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

It is a new request after the redirect. In order to access the result you need to sotre it in som kind of session or pass it again.

You can pass it like this, then it will be in $_GET

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

评论列表(0)

  1. 暂无评论