Hi! I've finally learned to create a very simple javascript game. I wanted users to be rewarded money based on the score. However, I can't seem to pass the javascript end variable to a php one. I know this is a lot of code, but would anyone mind helping me?
wordscramblegame.php
PHP Code:
<?php
class wordscramblegameController extends AppController{
const PARAM = "eid";
const PARAM2 = "confirm";
private $view;
private $subController;
private $explore;
public function __construct(){
parent::__construct("member");
$mysidia = Registry::get("mysidia");
if($this->action != "index"){
$this->explore = new Explore($mysidia->input->get("eid"));
if($this->explore->getExploreOwner() != $mysidia->user->username) throw new NoPermissionException("You do not have permission to manage the expeditions of other users.");
}
}
public function index(){
$mysidia = Registry::get("mysidia");
$document = $mysidia->frame->getDocument();
$document->setTitle("Game");
$stats = $_POST['earnedStats'];
$user = $mysidia->user->username;
$document->add(new Comment("{$stats}"));
$mysidia->db->update("users",array("money" => '{$stats}'),"username = '{$user}'");
$document->add(new Comment('
<DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.8.3.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<style>
#playingDisplay {
display: none;
}
#gameEndDisplay {
display: none;
}
#instructions {
display: none;
}
</style>
</head>
<body>
<div onload="printMsg()">
<div id="introScreen">
<button type="button" id="startButton" onclick="startGame()">Start</button>
<button onclick="openInstructions()">Instructions</button>
</div>
<div id="playingDisplay">
<div id="infoBox"></div>
<div id="scrambledWordBox"></div>
<div id="answerBox">
Unscramble the word:
<input type="text" id="myAnswer" value="">
<button onclick="submitAnswer()">Submit</button>
</div>
<div id="sideBox">
<div id="time">10:00</div>
<div id="scoreBox"></div>
<div id="moneyBox"></div>
<button onclick="quitGame()">Quit</button>
</div>
</div>
<div id="instructions">
Unscramble as many words as you can and input your answer into the field. You have 10 minutes to correct as many as you can!
<button onclick="hideInstructions()">Done</button>
</div>
<div id="gameEndDisplay">
<div id="endStats"></div>
<button onclick="replayGame()" id="replayButton">Replay</button>
</div>
</div>
<script>
var keepPlaying = true;
var answersCorrect = true;
var score = 0;
var money = 0;
function startTimer(duration, display) {
var timer = duration,
minutes, seconds;
setInterval(function() {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (--timer < 0) {
if (answersCorrect != false && keepPlaying != false) {
money = score * 10;
$("#playingDisplay").hide();
$("#endStats").html("Time is up! Your score is " + score + " and you have earned " + money + " fuzzpuffs");
$("#gameEndDisplay").show();
}
}
}, 1000);
}
function beginTimer() {
var tenMinutes = 60 * 10,
display = document.querySelector("#time");
startTimer(tenMinutes, display);
}
function startGame() {
beginTimer();
$("#playingDisplay").show();
$("#introScreen").hide();
}
if (answersCorrect != false && keepPlaying != false) {
var words = ["caterpillar", "butterfly", "flower"];
var word = words[Math.floor(Math.random() * words.length)];
String.prototype.shuffle = function() {
var a = this.split(""),
n = a.length;
for (var i = n - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
return a.join("");
}
var scrambledWord = word.shuffle();
(function printMsg() {
var scrambledWordBox = document.getElementById("scrambledWordBox");
scrambledWordBox.innerHTML = "<p>" + scrambledWord + "</p>";
var scoreBox = document.getElementById("scoreBox");
scoreBox.innerHTML = "<p>Score: " + score + "</p>";
var moneyBox = document.getElementById("moneyBox");
moneyBox.innerHTML = "<p>Money Earned: " + money + "</p>";
})();
function submitAnswer() {
var userAnswer = document.getElementById("myAnswer").value;
if (userAnswer == word) {
words = ["caterpillar", "butterfly", "flower"];
word = words[Math.floor(Math.random() * words.length)];
scrambledWord = word.shuffle();
score++;
money = score * 10;
$("#infoBox").html("Correct!");
$("#scrambledWordBox").html("<p>" + scrambledWord + "</p>");
$("#scoreBox").html("<p>Score: " + score + "</p>");
$("#moneyBox").html("<p>Money Earned: " + money + "</p>");
} else {
$("#playingDisplay").hide();
$("#endStats").html("Your answer was incorrect. Your score is " + score + " and you have earned " + money + " fuzzpuffs");
$("#gameEndDisplay").show();
answersCorrect = false;
}
}
}
function quitGame() {
var moneyEarned = money + score;
$("#playingDisplay").hide();
$("#endStats").html("Your score is " + score + " and you have earned " + money + " fuzzpuffs");
$.ajax({
type: "POST",
url: "wordscramblegame.php",
data: { earnedStats : moneyEarned },
success: function(data){
alert(data);
}
});
$("#gameEndDisplay").show();
keepPlaying = false;
}
function replayGame() {
location.reload();
}
function openInstructions() {
$("#instructions").show();
$("#introScreen").hide();
}
function hideInstructions() {
$("#instructions").hide();
$("#introScreen").show();
}
</script>
<body>
</html>
'));
}
}
?>