将本地存储变量发送到PHP

将本地存储变量发送到PHP

问题描述:

我想在页面加载时将本地存储变量传递给PHP.这是我尝试过的

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'];
?>

然后对其进行测试,我在配置文件PHP中 echo $ userMark .但是,虽然我获得了成功的使用marks.php响应执行某些操作"警报,但没有在 profile.php 上得到回显的$ 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.

有什么建议吗?

echo $ userMark marks.php

marks.php

marks.php

<?php

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

?>

profile.php

profile.php

并在javascript中警告数据

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");
        });

});