简体   繁体   中英

redirect page using ajax & jquery (php)

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 outcome 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?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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