I'm a total newbie to JavaScript, Ajax and PHP, so please excuse any stupid question I might ask.
I'd like to use a lazy loading TreeView which gets it's node data from a database via PHP. I found a JavaScript control to use and an example to try out, and now I have a question.
To lazy load the data for the expanded tree node I need to fire an Ajax request to a PHP file which gives me the answer as a JSON string. In the example I'm trying out this PHP file only delivers one string (with 'echo'), but what do I have to do if I need to loop through a database recordset. Can I 'echo' every partial answer to my lazy loading treeview as I get it from the database or do I need to collect the complete answer inside the PHP file in a string variable and send it only when it's complete?
I don't think that providing any code is helpful, because I think the question is a general question. Can I answer an Ajax request with partial answers and the answer is complete when the PHP file reached it's end or can I answer an Ajax request with only and exactly one complete answer?
I hope you understand my question.
Thanks for any answer or advice in advance.
If i understand your question correctly this is how you can send request and get json data using ajax and loop through it:
Html:
<input id="button" type="button" value="click">
target.php:
<?php
if(isset($_POST['submit']) and $_POST['submit']=='submit'){
$array=array(
array('name'=>'Rasmus','age'=>'32'),
array('name'=>'Jon','age'=>'43'),
array('name'=>'Lora','age'=>'35')
);
$json=json_encode($array);
echo $json;
}
Javascript:
$("#button").click(function(){
$.ajax({
type: 'POST',
url: 'target.php',
data: {submit:'submit'},
success:function(data) {
var rdata=$.parseJSON(data);
$.each(rdata,function(){
alert(this['name']+this['age']);
});
}
});
})