So I am making a Rock Paper Scissors game. I want to make it that when the user loses to the computer, the game finishes or restars and the user's high score is updated in MySQL. I cannot seem to make it work tho. This is what I have so far: The code is big so I will add a small part of it.
apps.js
function lose(user, computer) {
compScore++;
userScore_span.innerHTML = userScore;
compScore_span.innerHTML = compScore;
rezultat_p.innerHTML = convertWord(user) + " loses to " + convertWord(computer) + ". Game Over!";
getOutput();
}
function getOutput() {
jQuery.ajax({
type: "POST",
url: 'myAjax.php',
dataType: 'json',
data: {functionname: 'gameOver', arguments: [userScore]},
success: function (obj, textstatus) {
if( !('error' in obj) ) {
yourVariable = obj.result;
}
else {
console.log(obj.error);
}
}
});
}
myAjax.php
<?php
function gameOver($highScore){
session_start();
require_once 'dbh.inc.php';
$user = $_SESSION['userid'];
echo($user);
$sql = "UPDATE users SET userHighScore = $highScore WHERE userId = $user;";
$stmt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmt, $sql)){
header("location: ../leaderboard.php?error=stmtfailed");
exit();
}
mysqli_stmt_bind_param($stmt, "s", $highScore);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}
?>
So when I run this, the getOutput function executes but the PHP function doesn't seem to execute. Maybe it is an error in the PHP function, I am not sure.
I think that $user = $_SESSION['userid']; doesn't work properly.
gameOver? I see it passed as an argument in the data for your ajax call but not how that ajax call is processedfunctionnameparameter.myAjax.phpscript, you're only defining the function, not calling it. If there is more to that script, then please include that in the question.argumentsin your ajax function is an array - presumably that is a single value in the array or multiple? If it is single there should be no need for the array syntax and it'll ake the PHP processing easier