xkurohatox
xkurohatox

Reputation: 198

How to send localStorage from JS to PHP file (?)

I am trying to move localStorage content to a php email file. The localStorage was created with Javascript. The thankyou.php CAN use JS/jQuery BUT the email file (admin-new-order.php CANNOT use JS or jQuery...

Here is the code I am using. It's not working properly:

JS on thankyou.php (1st page):

  <script>
      var value = localStorage.totallocal;

      jQuery.post(
            "admin-new-order.php", 
            {myKey: value}, 
            function(data){
               var value = localStorage.totallocal;
            }).fail(function()
               {
                   alert("error");
            });
       }
  </script>

PHP on admin-new-order.php (2nd page/sent to email inbox). Retrieves value from thankyou.php:

    <p><?php  $value = $_POST["value"]; printf("%u",$value); ?></p>

The email file shows this: 0 but the value from localStorage should always be between 0.01 - 10000.00. I don't think it is receiving the localStorage...but I don't know what I am doing wrong. I am new to php.

Thank you in advance!

Upvotes: 1

Views: 2235

Answers (2)

Amrinder Singh
Amrinder Singh

Reputation: 5502

You cannot access localstorage via PHP. You need to write some javascript that sends the localstorage data back to the script.

If you are using jQuery, do something like the following.

set_page.php

<script>
localStorage.setItem('email', '<?php echo $_SESSION['email'];?>');  
localStorage.setItem('password', '<?php echo $_SESSION['password'];?>');
</script>

login_page.php

<script>
var email = localStorage.getItem('email'), password = localStorage.getItem('password');
$.POST('login_response.php', {'email':email,'password':password}, function(data){
  alert('Login Successful.  Redirect to a different page or something here.');
}
</script>

login_response.php

<?php
$email = $_POST['email'];
$password = $_POST['password'];
//Handle your login here.  Set session data, etc.  Be sure to sanitize your inputs.
?>

Upvotes: 0

Jagdish Idhate
Jagdish Idhate

Reputation: 7742

Instead of $_POST["value"] it should be $_POST["myKey"]

<p><?php  $value = $_POST["myKey"]; printf("%u",$value); ?></p>

Upvotes: 1

Related Questions