I am writing a tic tac toe game and implementing Ajax and Php, both of which are new to me. I have looked at a few other discussions but can't seem to find my answer. My js file is calling the php, which is in turn calling a database and pulling questions and answers--The user has to answer the question to play his/her turn. However, my program returns the answer to the next question asked, not the current question.
Here is my php:
$con = mysql_connect('localhost', '412ygutstei', '412ygutstei');
if ($con)
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$questionNum = $_POST['q_id'];
mysql_select_db("ygutstei_db", $con);
$result = mysql_query("SELECT q_id, question_type, text,description, answer FROM questions,questiontype WHERE q_id=".$questionNum."");
$row = mysql_fetch_array($result);
$question = $row['description']." ".$row['text']."?|".$row['answer'];
echo($question);
mysql_close($con);
my js functions, which will call php:
function countDown() {
var element = document.getElementById(elem);
element.innerHTML = secs;
if(secs == 15){
document.getElementById('playerDisplay').innerHTML= printEquation();
}
if(secs == 0){
clearInterval(clock);
turn++;
startTimer();
document.getElementById(answer).value = "";
}
else{
secs--;
}
}
var expect = ""; var randomQNumber; var correctAnswer; var askQuestion = "";
function printEquation(){
randomQNumber = randNumber(1,8);
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var elements = xmlhttp.responseText.split("|");
//elements[0] = document.getElementById("question")
correctAnswer = elements[1];
expect = correctAnswer;
askQuestion = elements[0];
//alert(correctAnswer);
}
}
xmlhttp.open("POST","G_TicTacToe.php",true);
xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xmlhttp.send("q_id="+randomQNumber);
if(turn % 2 == 0){
return "X Plays: " + askQuestion + " = ";
}
else{
return "O Plays: " + askQuestion + " = ";
}
}
function matchAnswer(a) {
var b = document.getElementById(a).value;
document.getElementById(a).innerHTML = b;
if(expect.toUpperCase() == b.toUpperCase()){
clearInterval(clock);
answeredQuestion = true;
window.alert("Correct! ");
document.getElementById(a).value = "";
click();
}
else{
clearInterval(clock);
window.alert("Incorrect! The correct answer was: " + expect);
document.getElementById(a).value = "";
playerTurn();
startTimer();
}
html code that calls matchAnswer():
<input type="text" id="answer">
<button id="equals" onclick="matchAnswer('answer');";>Answer</button>
</div>
<div class="playerAnswer">
</div>
ex: if output is: X Plays: What is the name of State1? - answer Illinois, it will return the answer for the next question, "What's my favorite animal? - i.e. penguin.
Any suggestions would be greatly appreciated. If more code is needed, please let me know.