Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting Ajax return data in jQuery

I have done jQuery and Ajax, but I am not able to get the response into a Div element. This is the code:

Index.html

$.ajax({
    type:"POST",
    url: "ajax.php",
    data:"id="+id ,
    success: function(html){
        $("#response").html(data);
    }
});

It is receiving the response to my <div id="response"></div>.

The ajax.php returns following code to the index.html file:

<div id ="one"> OneVal </div>
<div id ="sub"> SubVal </div>

Will I able to extract the OneVal and Subval into a variable, and how can I extract "OneVal" and "SubVal", instead of above response?

like image 548
venkatachalam Avatar asked Dec 30 '08 13:12

venkatachalam


3 Answers

Change the .find to .filter...

like image 190
redsquare Avatar answered Sep 22 '22 16:09

redsquare


I have noticed that your success function has the parameter "html", and you are trying to add "data" to your elements html()... Change it so these both match:

$.ajax({
    type:"POST",
    url: "ajax.php",
    data:"id="+id ,
    success: function(data){
        $("#response").html(data);
    }
});
like image 22
VinnyBenson Avatar answered Sep 26 '22 16:09

VinnyBenson


You can use json like the following example.

PHP code:

echo json_encode($array);

$array is array data, and the jQuery code is:

$.get("period/education/ajaxschoollist.php?schoolid="+schoolid, function(responseTxt, statusTxt, xhr){
    var a = JSON.parse(responseTxt);
    $("#hideschoolid").val(a.schoolid);
    $("#section_id").val(a.section_id);
    $("#schoolname").val(a.schoolname);
    $("#country_id").val(a.country_id);
    $("#state_id").val(a.state_id);
}
like image 21
aya Avatar answered Sep 24 '22 16:09

aya