doushan2811 2016-01-02 12:30
浏览 35
已采纳

从php页面使用数组结果html

I have the following code in my html page:

<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>

and then in my php page I have:

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

First of all, the php page simply returns "Array". Is this simply the name of the array, or will it be usable in the html page to get the values out of it?

Secondly, when I do "$responses.get..." I am hoping that it will create a variable "reponses," then set it to be whatever the php sends out (currently, key1 of the array is set to be whatever is inputted to the form, but I will update this later of course). It should then load what is in key1 of the array into div0. Not only is it not setting $responses as anything, but I am confident that even if it did, div0 would not be set as intended either. How do I solve this?

I was originally thinking I could do it more directly with:

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

but this didn't work so I decided to split the problem up, and as you can see had no luck there either.

Thanks in advance for your help.

____UPDATE1____

I have changed the code in the php page to this:

echo json_encode($array);

and the code in the html page to this:

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

which throws the following error: Uncaught TypeError: Cannot use 'in' operator to search for 'length' in {"key1":"","key2":"hello"}

alternatively, writing

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

throws the error: Uncaught Error: Syntax error, unrecognized expression: {"key1":"","key2":"hello"}

But this is progress! Thank you all so far for your helpful answers.

____UPDATE2____

I have decided to do this instead of iterating through the array:

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

where response will be something along the lines of {"key1":forminputhere,"key2":"hello","key3":forminputhere,"key4":"hello"}

However, response.key1, response.key2 etc are all "undefined". So this is clearly not how to extract elements from an array in html. response[0] gives "{" too, which is not ideal. Similar questions online all seem to involve iterating with each (as I did after update1, not that that was successful either!) - is what i'm trying to do even possible?

____UPDATE3____

Doing this:

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

just makes the $array = key1,key2,key3,key4, and completely ignores all values of the array.

  • 写回答

2条回答 默认 最新

  • doubi6898 2016-01-15 16:32
    关注

    ____UPDATE 4____

    $.get("url/here",$("#form0").serialize(), function(response){
        $.each(JSON.parse(response), function(index){
            $array.push(JSON.parse(response)[index])
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?