Jquery Get data is sending through anchor tag by click event. It returns data to anchor tag page correctly but when page is redirected it does not returns data to that page.
There is the anchor tag code. It is dynamically generated. After clicking this anchor tag jquery sends get data to LikeMail.php page. which then returns to redirected page (viewProfile.php) through ajax method
<a class="profile" href="<?php echo "viewProfile.php?id=" . $record['user_id'];?>" >
<img class="img-rounded"
src=" <?php echo "../shadi/images/" . $record['user_photo1'] ?>"
id="<?php echo $record['user_id']; ?>"alt="" width="70%" height="20%">
</a>
This is Jquery method by which data is sending by Get method
$(document).ready(function () {
$('.profile').on('click', function (e) {
//Get the href Link
var href = $(this).attr('href');
e.preventDefault();
$.get("LikeMail.php",
{user2: $(this).find(".img-rounded").attr("id")}, function (returnedData) {
// Do whatever you want with returend data
console.log(returnedData);
}).done(function() {
window.location.href = href;
});
});
});
This is ajax function which receives data in redirected page
function like()
{
var req = new XMLHttpRequest();
req.onreadystatechange = function()
{
if(req.readyState==4 && req.status==200)
{
document.getElementById('Like').innerHTML=req.responseText;
}
}
req.open('GET','LikeMail.php','true');
req.send();
}
setInterval(function(){like()},1000);
I want to know that why my jquery Get data returns to anchor tag page BUT why not on redirected page. I want to make it work on redirected page
Your data will not exist on the next page after redirect. You can save the data to the session, which will then make it available on the other page.
In your PHP script which receives the data on the first page, save the data you want to keep to the session. Here is a tutorial about sessions if you don't know how to do that.
Now on the other page all you need to do is read the session data which was saved. Looks like you could probably do that just on load of the page, using PHP.
If you want to initiate another ajax call to get the session data (I'm not sure why you want to do that, it doesn't seem like a good solution to anything I can think of) you need to make your ajax request to a PHP script which will return the $_SESSION
array you set in the previous page.