so I'm trying to get javascript variable value into hidden input field and it sort of work, but I get default variable value from html5games.pingpong.js. If I set variable myVar = 0; I get 0 in an input field, but it's a game and I can't get the value of bestScore to increment.
html5games.pingpong.js
var pingpong = {
scoreA : 0, // score for player A
scoreB : 0, // score for player B
bestScore : 0
};
function movePaddles() {
pingpong.bestScore += 5;
}
PHP: test.php
<input id="score" type="hidden" name="bestScore">
<script>
document.getElementById('score').value = pingpong.bestScore;
</script>
Here is an example using jQuery (you can try it at https://jsfiddle.net/07tb4bp9/):
var pingpong = {
scoreA : 0, // score for player A
scoreB : 0, // score for player B
bestScore : 0
};
function movePaddles() {
pingpong.bestScore += 5;
$('#score').val(pingpong.bestScore);
// Without jquery : document.getElementById('score').value= pingpong.bestScore;
}
movePaddles();
You need to update the value when it changes.
function movePaddles() {
pingpong.bestScore += 5;
document.getElementById('score').value = pingpong.bestScore;
}