I have the following javascript code. The variable dyna was originally retrieved from PHP. As the user clicks any of the buttons, the variable dyna increases/decreases by 1 with each click (depending on whether they hit the '-' button or the '+' button). My objective is to pass the javascript variable 'dyna' through a POST method to PHP.
Any advice on how this can be done?
<script>
var dyna = <?php echo $user_dyna; ?>
$(function() {
$(".numbers-row").append('<div class="inc button">+</div><div class="dec button">-</div>');
$(".button").on("click", function() {
var $button = $(this);
if ($button.text() == "+") {
dyna -= 1;
} else {
dyna += 1;
}
$.ajax({
url: 'stats.php',
type: 'POST',
data: {dyna: dyna}
});
});
});
</script>
dynais indeed updated at the time it's being sent back? And keep in mind thatstats.phpwill need to process thedynaPOST somehow --$_POST['dyna']should contain the value -- though keep in mind this happens in the background; you won't see it if you visit the page yourself. To know that it was sent successfully, you can check theresponseof the POST request from the JavaScript side of things.