Reputation: 237
How can I store this javascript data to a php variable on the same page. This is what I've tried.
index.php
PHP
//Receiving Data
$ajax_data = $_POST['data'];
echo $ajax_data;
JS
var fname = "Mike";
var form = {"first_name":fname};
person = JSON.stringify(form);
$.ajax({
url: "index.php",
method: "POST",
dataType: 'JSON',
data: ({
data: person
}),
});
</script>
Upvotes: 0
Views: 59
Reputation: 91
Store data through JavaScript cookie and get through PHP
<script>
var fname = "Mike";
document.cookie = 'first_name=' + fname; </script>
<?php echo $_COOKIE['first_name'];
?>
Upvotes: 1