2 Replies - 579 Views - Last Post: 29 August 2016 - 02:37 PM Rate Topic: -----

#1 Manven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-August 16

How can I return the html data using json encode and ajax

Posted 28 August 2016 - 01:29 PM

I'm trying to display data from the database using JSON encode function.But, I'm having difficulties on how set a variable that is capable of handling an array of the data.How can I display data inside "function tree()" by JSON encode function? Please help me.Thanks!


//index.php
//Javascript
 
	 function show_messages(){
		 $.ajax({
			 url: "index.php",
			 cache: false,
			 success: function(html){
				 $("#table_content").html(html);
			 }
		 });
	 }



//index.php

//PHP
include ("db_connect.php");
$query="SELECT * FROM `comments` ORDER BY id ASC";
$result = mysql_query($query);
if (isset($_REQUEST['AnswerId'])){
	$AnswerId = $_REQUEST['AnswerId'];	
}
else
{$AnswerId = 0; }

$i=0;
while ($mytablerow = mysql_fetch_row($result)){
	$mytable[$i] = $mytablerow;	
	$i++;	
}

function tree($treeArray, $level, $pid = 0){
	global $AnswerId;
	if (! $treeArray) 
	{ return; }	
	     foreach($treeArray as $item) 
	{
		if ($item[1] == $pid){
			?> 	
			<!-- Showing each comment with the correct indentation -->
			<div class="CommentWithReplyDiv" style="margin-left:<?php echo($level*60);?>px"> 	
			<div class="CommentDiv">
			<div class="User"><?php echo($item[2]) ; ?></div>
			<div class="Message"><?php echo($item[3]) ; ?></div>
			<div class="Date"><?php echo($item[4]) ; ?></div>
			<?php				
   if ($level<=40) 
  { echo '<a href="" class="ReplyLink" onclick="AnswerComment('.$item[0].');return false;">Reply</a>'; }
	echo '<a href="" class="DeleteLink" onclick="DeleteComment('.$item[0].');return false;">Delete</a>';
			?> </div> <?php
			
			if ($AnswerId == $item[0]){
				?><div id="InnerDiv"><?php
	            ShowForm($AnswerId);
				?></div><?php	
			} 
			
			?> </div> <?php 
			 echo ('<br/>');
			tree($treeArray, $level+1, $item[0]);
		}		
	}
}
tree($mytable, 0);
?>


Is This A Good Question/Topic? 0
  • +

Replies To: How can I return the html data using json encode and ajax

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: How can I return the html data using json encode and ajax

Posted 29 August 2016 - 12:35 PM

You need to return data instead of print it, so you would add it to an array. It seems like you made an attempt at making a recursive function, but it's unusual to keep passing the same data structure. Normally you would pass just the children that you're trying to print.
Was This Post Helpful? 1
  • +
  • -

#3 Manven   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-August 16

Re: How can I return the html data using json encode and ajax

Posted 29 August 2016 - 02:37 PM

Thank you for your advice. I hope this will help me alots to find the solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1