title: How to return output on the same page ajax php
body: <p>I want to organize the selection query from index.php to action, so i can be able to call the file with ajax to show the data on the page.(index.php). So, the activity I want to do is to able to submit data of the form to the database and to display result on the same page(index.php) without refreshing the page. Help please</p>
  
  <pre><code>Here's my files
  1.action.php
  2.index.php
  3.maker.js
  </code></pre>
  
  <p>//-----------------------action.php-----------------------------</p>
  
  <pre><code>&lt;?php
  include ("db_connect.php"); // Connecting to the database
  $user = $_REQUEST['user'];
  $text = $_REQUEST['text'];
  $ParentId = $_REQUEST['ParentId'];
  $action = $_REQUEST['action'];  
  
  if ($action=="add")
  {
      $query="INSERT into `comments` VALUES (NULL,'{$ParentId}','{$user}','{$text}',NOW())";
      $result = mysqli_query($conn,$query);
  }
  
  if ($action=="delete")
  {   
      $delete = "DELETE FROM `comments` WHERE id=$text";
      $result = mysqli_query ($conn,$delete);
  }
  ?&gt;
  </code></pre>
  
  <p>//index.php</p>
  
  <pre><code>&lt;div id="table_content"&gt;&lt;/div&gt;
  &lt;script type="text/javascript" src="maker.js"&gt;
  &lt;?php
  function ShowForm($AnswerCommentId) 
  {  ?&gt; 
  &lt;form id="myForm"&gt;  
  &lt;input type="hidden"  name="comment_on" id="comment_on"  readonly="readonly" value="&lt;?php print md5($_SERVER['PHP_SELF']); ?&gt;" /&gt;
  &lt;input id="user" name="user" value="name" autocomplete="off" onfocus="if(this.value == 'name'){this.value = ''}" onblur="if(this.value == ''){this.value = 'name'}"/&gt;               
  &lt;textarea id='text' name='text' value="comment" onfocus="if(this.value == 'comment'){this.value = ''}" onblur="if(this.value == ''){this.value = 'comment'}" &gt;&lt;/Textarea&gt;       
  &lt;input id="ParentId" name="ParentId" type="hidden" value="&lt;?php echo($AnswerCommentId);?&gt;"/&gt;
  &lt;button type='button' OnClick=SendComment()&gt;Comment&lt;/button&gt;
  &lt;/form&gt;
  &lt;?php
  }
  
  $query="SELECT * FROM `comments` ORDER BY id ASC";
  $result = mysqli_query($conn,$query);
  
  if (isset($_REQUEST['AnswerId']))
  { $AnswerId = $_REQUEST['AnswerId'];    }
  else
  { $AnswerId = 0; }
  
  $i=0;
  while ($mytablerow = mysqli_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)       
      {
          ?&gt;  
  &lt;div class="CommentWithReplyDiv" style="margin-left:&lt;?php echo($level*60);?&gt;px"&gt;    
          &lt;div class="CommentDiv"&gt;
          &lt;pre class="Message"&gt;&lt;?php echo($item[3]) ; ?&gt;&lt;/pre&gt;
          &lt;div class="User"&gt;&lt;?php echo($item[2]) ; ?&gt;&lt;/div&gt;
          &lt;div class="Date"&gt;&lt;?php echo($item[4]) ; ?&gt;&lt;/div&gt;
          &lt;?php               
  if ($level&lt;=4)  { echo '&lt;a href="" class="ReplyLink" onclick="AnswerComment('.$item[0].');return false;"&gt;Reply&lt;/a&gt;'; }
           echo '&lt;a href="" class="DeleteLink" onclick="DeleteComment('.$item[0].');return false;"&gt;Delete&lt;/a&gt;';
          ?&gt; &lt;/div&gt; &lt;?php
  
          if ($AnswerId == $item[0])
          {
          ?&gt;&lt;div id="InnerDiv"&gt;&lt;?php  ShowForm($AnswerId);  ?&gt;&lt;/div&gt;
  &lt;?php   
          } 
          ?&gt; &lt;/div&gt; &lt;?php 
  
          tree($treeArray, $level+1, $item[0]);   // Recursion
      }       
  }
  }
  tree($mytable, 0);
  ?&gt;
  </code></pre>
  
  <p>//maker.js</p>
  
  <pre><code>function DeleteComment(number){
  $.ajax({
      type: "POST",
      url: "action.php",                  
      data: "user=1"+"&amp;text="+number+"&amp;ParentId=1"+"&amp;action=delete",                  
      success: function(html){                        
          $("#table_content").html(html);         
         }
   });
  }
  
  function AnswerComment (id){
  $.ajax({
      type: "POST",
      url: "index.php",                   
      data: "AnswerId="+id,                   
      success: function(html){                        
          $("#table_content").html(html);             
         }
  });     
  }
  
  function SendComment (){
  var user1 = $("#user").val();
  var text1  = $("#text").val();  
  var ParentId1  = $("#ParentId").val() + ""; 
  $.ajax({
      type: "POST",
      url: "action.php",  
      cache: false,       
      data: "user="+user1+"&amp;text="+text1+"&amp;ParentId="+ParentId1+"&amp;action=add",        
      success: function(html){                
          $("#table_content").html(html); 
          clean_form();       
               }
  });
  return false;
  }
  </code></pre>