I have PHP function inside a class that returns a math problem:
public function level1() {
//level 1 and 2
//single digit addition and subtraction
//randomly choose addition or subtraction
//1 = addtion, 2 - subtraction
$opperand = rand( 1, 2 );
//if the problem is a subtraction, the program will keep generating problems if a negative problem is generated
//if opperand is a subtraction, do generate both numbers while the answer is negative
if ( $opperand == 2 )
{
do {
//randomly generate first number
$number1 = rand( 1, 9 );
//randomly generate second number
$number2 = rand( 1, 9 );
//compute the answer
$answer = $number1 - $number2;
//change variable to actual opperand
$opperand = "-";
} while ( $answer < 0 );
}
else
{//addition problem
//randomly generate first number
$number1 = rand( 1, 9 );
//randomly generate second number
$number2 = rand( 1, 9 );
//compute the answer
$answer = $number1 + $number2;
//change variable to actual opperand
$opperand = "+";
}//end if/else
return array( $number1 . " " . $opperand . " " . $number2 . " ", $answer );
I call this function from ajaxHandler.php (Which I call from ajax)
$problemData = $MathEngine->level1();
return $problemData;
The php will always return an array, but I cannot figure out how to manipulate or even view the results as an array in javascript. Is there a way to do this? I've used the standard Get ajax call before so that's not new. when I try to reference the ajax response text as an array, I either get nothing (when i click the button) or 'undefined'
var problemData = ajaxRequest.responseText;
alert( problemData[0] )