enance enance - 1 month ago 4x
PHP Question

Hiding php echo output with json and ajax request

I'm having the problem running php script right when the page loads, and the output is then displayed on the page, which I dont actually want to display. I tried to hide echo with.

<div style='display:none>

but, still doesn't work. I have an idea that "Ajax and JSON Encode function()" will help me to solve this issue of 'making ajax request to same page'. But, I real don't know how to create an array to encode html and php variable that are responsible for the results. Help please.

Here's my ajax function to show the output.

function mass()
url: "page.php",
cache: false,
success: function(html){

Here's my php script that hold the output of the page

$query="SELECT * FROM `comments` ORDER BY id ASC";
$result = mysql_query($query);

if (isset($_REQUEST['AnswerId'])){ $AnswerId = $_REQUEST['AnswerId']; }
{ $AnswerId = 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){
<div class="Div" style="margin-left:<?php echo($level*60);?>px">
<div class="CotDiv">
<div class="ser"><?php echo($item[2]) ; ?></div>
<div class="Mse"><?php echo($item[3]) ; ?></div>
<div class="ito"><?php echo($item[4]) ; ?></div>
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);


@enance this isn't the exact same code, but the idea is the same. This probably doesn't output correctly because I don't have an example array of the mysql output to test.

Just a few notes, try not to use globals and just pass the variable into the function. For the sake of time, I kept the output of html within this function, but ideally you'll like to output html through another function.

// example array
$mytable = [

$answer_id = 1;

function tree($answer_id, $items, $level, $pid = 0) {
  if (empty($array))

  foreach ($items as $item) {

    if ($item[1] == $pid) {
      echo sprintf('<div class="Div" style="margin-left:%spx">',$level * 60);
      echo '<div class="CotDiv">';
      echo sprintf('<div class="ser">%s</div>', $item[2]);

    if ($level <= 40) {
      echo sprintf('<a href="" class="ReplyLink" onclick="AnswerComment(%s);return false;">Reply</a>', $item[0]);
      echo sprintf('<a href="" class="DeleteLink" onclick="DeleteComment(%2);return false;">Delete</a>', $item[0]);

    echo '</div>';

    if ($answer_id == $item[0]) {
      echo sprintf('<div id="InnerDiv">%s</div>', 'FORM OUTPUT HERE');
      tree($answer_id, $items, $level++, $item[0]);

    echo '</div>';


tree($answer_id, $mytable, 0);

I really hope this helps to get you on the right track.