Answer the question
In order to leave comments, you need to log in
How to pass value from ajax to another page?
Hello.
Please let me know how this is done correctly.
I am getting a value on one page and want to pass that value to another page.
<script>
$(document).ready(function(){
$("#click_btn" ).click(function(){
var num = 12345;
console.log(num);
$.ajax({
url: "test.php",
cache: false,
data: ({type:"getnum", num:num}),
success: function(html){
setTimeout(function(){ document.location.href = "site.ru/test.php"; }, 2000);
}
});
return false;
});
});
</script>
<?php
if($_GET['type'] == 'getnum') {
$num = $_GET['num'];
echo $num;
}
?>
Answer the question
In order to leave comments, you need to log in
If this is some simple data, like name, phone number, etc., then use query parameters:
success: function(response){
setTimeout(function() {
document.location.href = `site.ru/test.php?number=${response.number}&name=${response.name}`;
}, 2000);
}
success: function(response){
localStorage.setItem('some_key', JSON.stringify(response.data));
setTimeout(function() {
document.location.href = 'site.ru/test.php';
}, 2000);
}
let helloHabr = JSON.parse(localStorage.getItem('some_key'));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question