user3762051
user3762051

Reputation: 57

Local storage javascript variable to php

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

Answers (1)

martin
martin

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

Related Questions