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

w1n*_*ter 1 html php arrays jquery

我的 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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

我原本以为我可以更直接地做到这一点:

$("#div0").load("*url here*",$("#form0").serialize())[key1];
Run Code Online (Sandbox Code Playgroud)

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

在此先感谢您的帮助。

____更新1____

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

echo json_encode($array);
Run Code Online (Sandbox Code Playgroud)

以及 html 页面中的代码:

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

抛出以下错误:未捕获类型错误:无法使用“in”运算符在{“key1”:“”,“key2”:“hello”}中搜索“长度”

或者,写

$(response).each(function(index){
            $("#div0").load(response);
        });
Run Code Online (Sandbox Code Playgroud)

抛出错误:未捕获错误:语法错误,无法识别的表达式:{“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;                
    });
Run Code Online (Sandbox Code Playgroud)

其中响应将类似于 {"key1":forminputhere,"key2":"hello","key3":forminputhere,"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)                  
        });
    });
Run Code Online (Sandbox Code Playgroud)

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

Zar*_*tra 5

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

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

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

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

json_encode($array);
Run Code Online (Sandbox Code Playgroud)

这会将您的数组编码为 json 响应,然后您可以回显该响应

echo json_encode($array);
Run Code Online (Sandbox Code Playgroud)

现在,在您的 JavaScript 中,您需要拦截此返回值。你正在做的事情不是做事的方法。

您有以下内容

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

这根本行不通。$responses 没有在任何地方定义。您正在寻找的是 jQuery 函数 .get(),因此您只需要 $.get()。$.get() 函数需要几个参数,即你的响应回调,最重要的是

$.get('url/here', function(response) {
    // stuff
});
Run Code Online (Sandbox Code Playgroud)

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

$(response).each(function(val) {
    //do stuff
});
Run Code Online (Sandbox Code Playgroud)

在继续之前,请查看$.get() 上的 jQuery 文档,我还会阅读更多有关 AJAX 的内容。