sauwbu sauwbu - 3 months ago 8
JSON Question

How to pass HTML with json and ajax

I'm trying to display the output from the database using JSON Encode function and Ajax. But, I'm getting difficult on how to pass the html element "" and the php "eg." variable that show data from the server.I want the output to be displayed using json and ajax request.Thanks.

Here's my codes

//js
function mpo()
{
$.ajax({
url: "main.php",
cache: false,
success: function(html){
$("#show_div").html(html);
}
});
}


//php

$bu = "<div class='main' style='margin-left:" .($level*60). "px'>";
$bu = "<div class='main_one'>";
$bu = "<div class='main_User'>" .($same[2]) . "</div>";
$bu = "<div class='main_Msg'>" .($same[3]) . "</div>";
$bu = "<div class='main_Date'>" . ($same[4]) . "</div>";
$json =array('bu'=>$bu);
echo json_encode($json);

Answer

You can use dataType : 'html' with this you don't requied to json encode

//js
  function mpo() 
 {
     $.ajax({
         url: "main.php",
         cache: false,
         dataType : 'html'
         success: function(html){
             $("#show_div").html(html);
         }
     });
 }

//php

$bu = "<div class='main' style='margin-left:" .($level*60). "px'>";     
$bu = "<div class='main_one'>";
$bu = "<div class='main_User'>" .($same[2]) . "</div>";
$bu = "<div class='main_Msg'>" .($same[3]) . "</div>";
$bu = "<div class='main_Date'>" . ($same[4]) . "</div>";

echo $json;die;
Comments