Use Localstorage Variables in PHP

Is it possible to store a resulting php variable into localStorage?

You can store directly like this, as long as you got PHP has this data with you in the same page

<script>
localStorage.setItem('php_var', "<?php echo $var;?>");
</script>

Sending a localstorage variable to PHP

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

});

Get localStorage value into php

You can not directly get a localStorage value in PHP, when script is executed, because localStorage is contained in user's browser, and PHP is running on server. However, you can send the value from localStorage to a server with GET or POST request.

Send Javascript local storage variables to MySql database using JQuery

When using Ajax I recommend trying the non-shorthand:

   $.ajax({
type: "POST",
url: 'auditConfirm.php',
data: {
jsAuditPercentage: localAuditPercentage,
jsVenuePercentage: localVenuePercentage,
}
});

This solution works on my end.



Related Topics



Leave a reply



Submit