Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP将数组返回到JavaScript Ajax调用_Php_Javascript_Ajax_Arrays - Fatal编程技术网

PHP将数组返回到JavaScript Ajax调用

PHP将数组返回到JavaScript Ajax调用,php,javascript,ajax,arrays,Php,Javascript,Ajax,Arrays,我在一个返回数学问题的类中有一个PHP函数: 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

我在一个返回数学问题的类中有一个PHP函数:

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 );
php总是会返回一个数组,但我不知道如何操作,甚至无法在javascript中将结果作为数组查看。有办法做到这一点吗?我以前使用过标准的Get-ajax调用,所以这不是什么新鲜事。当我尝试将ajax响应文本作为数组引用时,我要么什么也得不到(当我单击按钮时),要么“未定义”

           var problemData = ajaxRequest.responseText;

           alert( problemData[0] )

尝试
echo$problemData而不是返回它。
调用
警报(problemData[0])
时出现了什么错误? ajax只捕获字符串或json对象,所以唯一的方法是将该数组作为字符串返回并在js中拆分,或者在php端的该数组上使用json_编码

var data = problemData.split(' ');
alert(data[0]);

尝试
echo$problemData而不是返回它。
调用
警报(problemData[0])
时出现了什么错误? ajax只捕获字符串或json对象,所以唯一的方法是将该数组作为字符串返回并在js中拆分,或者在php端的该数组上使用json_编码

var data = problemData.split(' ');
alert(data[0]);

我会使用JSON。如果您以前从未听说过JSON,那么它只是一种在语言/平台之间来回发送内容的简单方法

在PHP脚本中,添加此代码段以JSON编码文本的形式回显数组。对AJAX请求的响应将是您响应的任何内容

// End of PHP script
$problemData = $MathEngine->level1();
$tmpOut = '{"bind":'. json_encode(array("problemData" => $problemData)) .'}';
echo $tmpOut;
exit;
现在,在Javascipt中,解码JSON字符串

// Javascript
var jsonObj=eval("("+ajaxRequest.responseText+")");
var problemData = jsonObj.bind.problemData;

我会使用JSON。如果您以前从未听说过JSON,那么它只是一种在语言/平台之间来回发送内容的简单方法

在PHP脚本中,添加此代码段以JSON编码文本的形式回显数组。对AJAX请求的响应将是您响应的任何内容

// End of PHP script
$problemData = $MathEngine->level1();
$tmpOut = '{"bind":'. json_encode(array("problemData" => $problemData)) .'}';
echo $tmpOut;
exit;
现在,在Javascipt中,解码JSON字符串

// Javascript
var jsonObj=eval("("+ajaxRequest.responseText+")");
var problemData = jsonObj.bind.problemData;

您可以使用json对象将数据从javascript(AJAX)发送和接收到php。使用json_encode()对来自php的数据进行编码,然后以html或文本的形式将其传递到javascript。javascript然后调用json_decode来检索数据并显示。

您可以使用json对象将数据从javascript(AJAX)发送和接收到php。使用json_encode()对来自php的数据进行编码,然后以html或文本的形式传递到javascript。javascript然后调用json_decode来检索数据并显示。

你能链接到深入教程吗?你能链接到深入教程吗?我将return改为echo,它在firefox中返回“it's wird,I get<,在IE中返回“undefined”。如果我将它改为[1],我在firefox上得到“b”,在IE上得到“undefined”。我看到了你的编辑,我要试试看。我将返回改为echo,它返回“it's怪异,我在firefox中得到<,在IE中得到“undefined”。如果我将其改为[1],我在firefox上得到“b”,在IE上得到“undefined”。我看到了你的编辑,我将在ajaxHandler.phpYes中尝试使用它,您可以在其中回显或返回json_编码。您必须使用“echo”、“print”或任何返回字符串的内容。如果要在ajaxHandler.phpYes中回显或返回json_编码,您可以。您必须使用“echo”、“print”或任何返回字符串的内容。