HTML in JSON Array Object

Can I have HTML injected into a JSON Array Object? I’m trying to add in HTML through AJAX to update my pagination but I get a jquery error. I’m using the php json encode function, but the injected HTML for the pagination is creating an error. Here is my ajax file.


if (isset($user_info['uid'], $_GET['id'], $_GET['page'], $_GET['s']) && $_GET['s'] === session_id())
{	
	//delete_conversation($_GET['id']);
	
	$total 					= fetch_total_conversations();
	$conversations 			= fetch_conversations($_GET['page'], 20);
	
	foreach($conversations as $k => $r)
	{
		$conversations[$k]['last_message'] = time_since_tstamp($r['last_message']).' ago';
	}
	
	$pages = new Paginator();
	$pages->items_total = $total;
	$pages->mid_range = 9;
	$pages->paginate(20);
	
	$conversations['pagination'] = $pages->display_pages();
	
	//print_array($conversations);
	echo json_encode($conversations);
}

Have you tried adding a die; after the echo? If that doesn’t work maybe post your error message.

Hi,
What do you see in the response (by firebugs) ?
Usually in this case there is a php error in the ajax file.

That didn’t work. This is what I am injecting into the array.

“<a class=“current” href=”#“>1</a> <a class=“paginate” href=”/assets/ajax/pm_delete_message.php?page=1&ipp=All&s=nd5qqqjoduk4cvfmumrq86qi57&id=110">All</a> "

This is the jquery error: “Cannot read properly length of undefined”.

Would it help if I printed the array for you?

No… no php error in the file. The php seems to be correct.

Here is the printed array. Pagination is at the bottom.

<pre>Array
(
[0] => Array
(
[id] => 110
[last_message] => 24 Minutes ago
[read] => 1
[replies] => 0
[subject] => fjs
[tags] => Array
(
)

        [users] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [uid] =&gt; 4
                        [username] =&gt; daveykult
                        [firstname] =&gt; Davey
                        [lastname] =&gt; Kult
                        [avatar] =&gt; /assets/img/avatars/users/daveykult.png
                    )

            )

    )

[1] =&gt; Array
    (
        [id] =&gt; 108
        [last_message] =&gt; 25 Minutes ago
        [read] =&gt; 1
        [replies] =&gt; 0
        [subject] =&gt; jks
        [tags] =&gt; Array
            (
            )

        [users] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [uid] =&gt; 4
                        [username] =&gt; daveykult
                        [firstname] =&gt; Davey
                        [lastname] =&gt; Kult
                        [avatar] =&gt; /assets/img/avatars/users/daveykult.png
                    )

            )

    )

[2] =&gt; Array
    (
        [id] =&gt; 107
        [last_message] =&gt; 25 Minutes ago
        [read] =&gt; 1
        [replies] =&gt; 0
        [subject] =&gt; fkjfd
        [tags] =&gt; Array
            (
            )

        [users] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [uid] =&gt; 4
                        [username] =&gt; daveykult
                        [firstname] =&gt; Davey
                        [lastname] =&gt; Kult
                        [avatar] =&gt; /assets/img/avatars/users/daveykult.png
                    )

            )

    )

[3] =&gt; Array
    (
        [id] =&gt; 106
        [last_message] =&gt; 25 Minutes ago
        [read] =&gt; 1
        [replies] =&gt; 0
        [subject] =&gt; kfjs
        [tags] =&gt; Array
            (
            )

        [users] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [uid] =&gt; 4
                        [username] =&gt; daveykult
                        [firstname] =&gt; Davey
                        [lastname] =&gt; Kult
                        [avatar] =&gt; /assets/img/avatars/users/daveykult.png
                    )

            )

    )

[4] =&gt; Array
    (
        [id] =&gt; 105
        [last_message] =&gt; 25 Minutes ago
        [read] =&gt; 1
        [replies] =&gt; 0
        [subject] =&gt; fkjs
        [tags] =&gt; Array
            (
            )

        [users] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [uid] =&gt; 4
                        [username] =&gt; daveykult
                        [firstname] =&gt; Davey
                        [lastname] =&gt; Kult
                        [avatar] =&gt; /assets/img/avatars/users/daveykult.png
                    )

            )

    )

[5] =&gt; Array
    (
        [id] =&gt; 104
        [last_message] =&gt; 25 Minutes ago
        [read] =&gt; 1
        [replies] =&gt; 0
        [subject] =&gt; fkl
        [tags] =&gt; Array
            (
            )

        [users] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [uid] =&gt; 4
                        [username] =&gt; daveykult
                        [firstname] =&gt; Davey
                        [lastname] =&gt; Kult
                        [avatar] =&gt; /assets/img/avatars/users/daveykult.png
                    )

            )

    )

[6] =&gt; Array
    (
        [id] =&gt; 103
        [last_message] =&gt; 25 Minutes ago
        [read] =&gt; 1
        [replies] =&gt; 0
        [subject] =&gt; cjdk
        [tags] =&gt; Array
            (
            )

        [users] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [uid] =&gt; 4
                        [username] =&gt; daveykult
                        [firstname] =&gt; Davey
                        [lastname] =&gt; Kult
                        [avatar] =&gt; /assets/img/avatars/users/daveykult.png
                    )

            )

    )

[7] =&gt; Array
    (
        [id] =&gt; 102
        [last_message] =&gt; 26 Minutes ago
        [read] =&gt; 1
        [replies] =&gt; 0
        [subject] =&gt; te
        [tags] =&gt; Array
            (
            )

        [users] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [uid] =&gt; 4
                        [username] =&gt; daveykult
                        [firstname] =&gt; Davey
                        [lastname] =&gt; Kult
                        [avatar] =&gt; /assets/img/avatars/users/daveykult.png
                    )

            )

    )

[8] =&gt; Array
    (
        [id] =&gt; 101
        [last_message] =&gt; 26 Minutes ago
        [read] =&gt; 1
        [replies] =&gt; 0
        [subject] =&gt; t
        [tags] =&gt; Array
            (
            )

        [users] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [uid] =&gt; 4
                        [username] =&gt; daveykult
                        [firstname] =&gt; Davey
                        [lastname] =&gt; Kult
                        [avatar] =&gt; /assets/img/avatars/users/daveykult.png
                    )

            )

    )

[9] =&gt; Array
    (
        [id] =&gt; 100
        [last_message] =&gt; 26 Minutes ago
        [read] =&gt; 1
        [replies] =&gt; 0
        [subject] =&gt; te
        [tags] =&gt; Array
            (
            )

        [users] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [uid] =&gt; 4
                        [username] =&gt; daveykult
                        [firstname] =&gt; Davey
                        [lastname] =&gt; Kult
                        [avatar] =&gt; /assets/img/avatars/users/daveykult.png
                    )

            )

    )

[10] =&gt; Array
    (
        [id] =&gt; 99
        [last_message] =&gt; 26 Minutes ago
        [read] =&gt; 1
        [replies] =&gt; 0
        [subject] =&gt; te
        [tags] =&gt; Array
            (
            )

        [users] =&gt; Array
            (
                [0] =&gt; Array
                    (
                        [uid] =&gt; 4
                        [username] =&gt; daveykult
                        [firstname] =&gt; Davey
                        [lastname] =&gt; Kult
                        [avatar] =&gt; /assets/img/avatars/users/daveykult.png
                    )

            )

    )

[pagination] =&gt; &lt;a class="current" href="#"&gt;1&lt;/a&gt; &lt;a class="paginate" href="/assets/ajax/pm_delete_message.php?page=1&ipp=All&s=584vgscq6oj2igpn437i1k67b4&id=110"&gt;All&lt;/a&gt; 

)
</pre>

May be a on line working link could help.
As blain advise put a die/exit at the bottom of
your php file overall is best practice as well.

I solved this last night. I needed to include the pagination array abject into the array. It was adding itself outside of it before.

Add an HTTP header as well:


header('Content-Type: application/x-json');
echo json_encode($data);
exit();

Be sure nothing else gets echo’d before or after the JSON content.