AJAX Not Printing What does PHP return?


So I am using ajax to print out a string received from my php file. The php file is creating rows for an html table. I am asking ajax to print these rows inside a with a specific id that is inside of a and i want it to print the rows when the page first loads up. My jQuery code is as follows:

    data: {action: 'name of function'},
    dataType: 'json',
    type: 'post',
    success: function(result){

And I want it to print out the returned data here in my html:

    <div id='div1'>

My PHP File basically does the following:

$htmlStr .= '<tr>';
$htmlStr .= '<td>Some Data</td>';
$htmlStr .= '<td class="text-center">' . some more data . '</td>';
$htmlStr .= '<td class="text-right">Rest of Data</td>';
$htmlStr .= '</tr>';

and I am returning that as follows:echo json_encode($htmlStr);

I am not sure what I am doing wrong or why it is not printing out, I know it is using my php file because it returned errors in my php code before when I had bugs that I fixed, but now the bugs are fixed so there are no more errors, but the code still isn't being printed out. Please Help thank you!

Use $('#div1').html(result); instead. By $('#div1') you are getting the element as jQuery object, that's why it was not working.

Also, if you've json encoded htmlStr you should store the html in an array and then encode it. Like:

    $response['html'] = '<h1>Hello World</h1>';
    $response['success'] = true;
    echo json_encode($response);

Or you could just use plain html response without json_encode and removing the dataType from your ajax call.

I hope it helps.