SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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.

    Code:
    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);
    }

  2. #2
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried adding a die; after the echo? If that doesn't work maybe post your error message.
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    What do you see in the response (by firebugs) ?
    Usually in this case there is a php error in the ajax file.

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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?

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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] => Array
    (
    [0] => Array
    (
    [uid] => 4
    [username] => daveykult
    [firstname] => Davey
    [lastname] => Kult
    [avatar] => /assets/img/avatars/users/daveykult.png
    )

    )

    )

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

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

    )

    )

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

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

    )

    )

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

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

    )

    )

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

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

    )

    )

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

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

    )

    )

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

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

    )

    )

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

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

    )

    )

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

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

    )

    )

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

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

    )

    )

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

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

    )

    )

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

    )
    </pre>

  6. #6
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    167
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I solved this last night. I needed to include the pagination array abject into the array. It was adding itself outside of it before.

  8. #8
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whisher View Post
    As blain advise put a die/exit at the bottom of
    your php file overall is best practice as well.
    Add an HTTP header as well:

    Code:
    header('Content-Type: application/x-json');
    echo json_encode($data);
    exit();
    Be sure nothing else gets echo'd before or after the JSON content.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •