Reputation: 57
Thanks for your time in reading this post.
I am facing issues while passing javascript variable to php file.
Please look at the below code for your reference which i have written in checkout.php.
$.ajax({
type: "POST",
url: "checkout.php",
data: localStorage.getItem("simpleCart_items"),
success: function(data) {
console.log("result"+JSON.stringify(data))
My php code :
<?php $data=$_POST['data']; echo $data;?>
Redirect is happening but $data value is null.
Upvotes: 0
Views: 223
Reputation: 96959
The { data: data }
is passed only to the first checkout.php
call using POST method. When you redirect the user with window.location.href
it has nothing to do with the previous call.
Depending on your use-case you could in the first call to checkout.php
save data
to session and the access it in the second call but it really depends what you're trying to do.
Edit: jQuery's data parameter is the entire payload you want to send to server. So if you were sending:
$.post({
data: {
message: 'Hello'
}
...
});
It'll be available in checkout.php
as $_POST['message']
and not $_POST['data']['message']
.
Upvotes: 1