0

I want to pass a local storage variable to PHP when the page loads. Here is what I tried

profile.php

$(document).ready(function(){              
    var userMarkVar = localStorage.getItem("userMark");
    jQuery.post("marks.php", {userMark: userMarkVar}, function(data){
        alert("Do something with marks.php responses");
    }).fail(function(){
        alert("Error");
    });
});

marks.php

<?php    
     $userMark = $_POST['userMark'];
?>

And then to test it, I echo $userMark in profile PHP. However whilst I get the success "Do something with marks.php responses" alert, I do not get the $userMark value echoed on profile.php.

Any suggestions?

3
  • suggestion: do a var_dump($_POST) to see what you've got. If there's no value check your networktab to see what get's actually sent.
    – Jeff
    Apr 14, 2018 at 9:03
  • "I echo $userMark in profile php": where? right next to the alert()??
    – Jeff
    Apr 14, 2018 at 9:06
  • @Jeff So i did a var_dump, and got "array(0) { }" as the result. And i have the above script in the <head> with the <?php echo $userMark ?> in the <body>
    – Adnan
    Apr 14, 2018 at 9:09

4 Answers 4

2

echo $userMark in marks.php

marks.php

<?php

 echo $userMark = $_POST['userMark'];
 echo $userMark;

?>

profile.php

and alert data in javascript

 $(document).ready(function(){

        var userMarkVar = localStorage.getItem("userMark");

        jQuery.post("marks.php", {userMark: userMarkVar}, function(data){
          alert(data);
        }).fail(function(){
          alert("Error");
        });

});

5
  • Thanks, this works as I get an alert with the localstorage data. However, how can i confirm that this is being sent to the php file? As i'd like to later send the data in the php file to a mysql database
    – Adnan
    Apr 14, 2018 at 9:12
  • the alert(data) already is the data from php sent back. So that's your confirmation.
    – Jeff
    Apr 14, 2018 at 9:15
  • u will get all variable send by javascript in marks.php where u can process the data to insert to database and then alert for successful additiion
    – Pradeep
    Apr 14, 2018 at 9:15
  • whatever u echo in marks.php it will show in the alert it is the response back to javascript ajax
    – Pradeep
    Apr 14, 2018 at 9:17
  • Great thanks, just checked it with my database - it works! And yes ofc! @pradeep
    – Adnan
    Apr 14, 2018 at 9:31
1

your marks.php should look like this:

<?php

 echo $userMark = $_POST['userMark'];
 echo $userMark;
?>

Then you can access userMark from data on your script

1

I run your code and everythings worked well ,

if you want to get the datas you use

alert(data);

only

0

This cannot work! The script is on the client side and PHP is on the server side! You have to load your data in a HTML form and post it to your PHP file. Then only you can get it with $_POST[...]!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.