在php页面的html中使用数组结果


Using array result in html from php page

我的html页面中有以下代码:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#button0").click(function(){
        $responses.get("*url here*",$("#form0").serialize());
        $("#div0").load($responses[key1]);
    });
});
</script>
</head>
<body>
<div id="div0">One response will appear here</div>
<form id="form0">
    <input type="text" name = "query" />
    <button type="button" id="button0"> enter your question here </button>
</form>
</body>

然后在我的php页面中,我有:

<?php
$query = $_GET['query'];
$array = array(
        key1 => $query,
        key2 => "hello"
);
echo $array;
?>

首先,php页面简单地返回"Array"。这只是数组的名称,还是可以在html页面中使用它来获取值?

其次,当我执行"$responses.get…"时,我希望它会创建一个变量"reponses",然后将其设置为php发送的任何值(目前,数组的key1设置为输入到表单的任何值,但我稍后当然会更新它)。然后,它应该将数组的key1中的内容加载到div0中。它不仅没有将$responses设置为任何内容,而且我相信即使设置了,div0也不会按预期设置。我该如何解决此问题?

我最初认为我可以更直接地使用

$("#div0").load("*url here*",$("#form0").serialize())[key1];

但这不起作用,所以我决定把问题分开,正如你所看到的,在那里也没有运气。

提前感谢您的帮助。

____更新1____

我已经将php页面中的代码更改为:

echo json_encode($array);

以及html页面中的代码到此:

$.get("url/here",$("#form0").serialize(), function(response){
        $.each(response, function(index){
            $("#div0").load(response);
        });
    });

其抛出以下错误:未捕获的类型错误:无法使用"in"运算符在{"key1":","key2":"hello"}中搜索"length"

或者,写入

$(response).each(function(index){
            $("#div0").load(response);
        });

引发错误:未捕获错误:语法错误,无法识别的表达式:{"key1":","key2":"hello"}

但这是进步!到目前为止,感谢大家的帮助。

____更新2____

我决定这样做,而不是在数组中迭代:

$.get("url/here",$("#form0").serialize(), function(response){   
        $("#div0").load(response.key1);
        $div0answer = response.key2;
        $("#div1").load(response.key3);
        $div1answer = response.key4;                
    });

其中响应将是沿着{"key1":forminuthere、"key2":"hello"、"key3":forminguthere和"key4":"hello"}

但是,response.key1、response.key2等都是"未定义的"。因此,这显然不是从html中的数组中提取元素的方法。response[0]也给出了"{",这并不理想。类似的在线问题似乎都涉及到对每个问题进行迭代(就像我在update1之后所做的那样,也不是成功的!)-我试图做的事情可能吗?

____更新3____

这样做:

$.get("http://community.dur.ac.uk/matthew.poyser/assessment.php",$("#form0").serialize(), function(response){
        $.each(JSON.parse(response), function(index){
            $array.push(index)                  
        });
    });

只是使$array=key1、key2、key3、key4,并完全忽略数组的所有值。

这是因为您只使用echo$array,它只打印出对数组的引用。

如果需要输出数组的内容,则需要使用print_r($array)或vardump($array)

然而,这看起来并不是你想要的。

您似乎想要的是让PHP脚本向您的AJAX调用输出一些东西,对吗?好吧,你需要用jQuery能理解的方式发回你的响应,而PHP var转储不是你想要的方式。您需要在PHP中使用一个非常重要的函数json_encode(),它将接受数组

json_encode($array);

这将把你的数组编码为json响应,然后你可以回显

echo json_encode($array);

现在,在您的javascript中,您需要截取这个返回值。你所做的并不是正确的方法。

您有以下

$(document).ready(function(){
    $("#button0").click(function(){
        $responses.get("*url here*",$("#form0").serialize());
        $("#div0").load($responses[key1]);
    });
});

这根本不起作用$任何地方都没有定义响应。你要找的是jQuery函数.get(),所以你只需要$.get(

$.get('url/here', function(response) {
    // stuff
});

通过回调传递到函数中的响应变量包含我们在使用echo-json_encode($array)之前输出的json响应。从那里,您可以将响应用作散列,或者用作常规json对象。哈希版本在中迭代非常简单

$(response).each(function(val) {
    //do stuff
});

在继续之前,请在$.get()上签出jQuery文档,我也会阅读更多关于AJAX的内容。

____更新4____

$.get("url/here",$("#form0").serialize(), function(response){
    $.each(JSON.parse(response), function(index){
        $array.push(JSON.parse(response)[index])
    });
});