title: Ajax response PHP echo HTML
body: <p><strong>Main issue:</strong> How do I properly echo out the HTML in my PHP from Ajax? My current webpage loads the php echo in the bottom of the page and not between the <code>&lt;tbody&gt;</code> tags.</p>
  
  <p>Basically, Ajax calls PHP file which echoes out HTML. My script is inside a table body. My Php then continues the table format adding rows. What I want to happen is a weekly schedule being displayed, which the displayed week can be manipulated by a button. Can my schedule be dynamically changed and not ruin the formatting?</p>
  
  <p>The following code is in the webpage I am loading up.</p>
  
  <pre><code>     &lt;tbody&gt;
          &lt;script&gt;
              function loadWeek(x) {
                 $.ajax({
                    url:"weekly.php",
                    type: "post",
                    data: {x}, 
                    success: function(response) { 
                      $(document.body).append(response);                                    }
                });
              }
              window.onload=loadWeek(1); //loads the first week of the schedule
          &lt;/script&gt;
      &lt;/tbody&gt;
  </code></pre>
  
  <p>Also, will the week incrementer/decrementer that calls the Ajax function ruin the formatting? Do I have to reload the page with that specific week?</p>
  
  <p>EDIT: words</p>
  
  <p>Alright let me explain. I wanted to load the 1st weeks schedule when the html page loads up initially. Then there are buttons under the weekly schedule the change the week, thus the week we want to view.</p>
  
  <p>This script is in the bottom of the code.</p>
  
  <pre><code>&lt;script&gt;
      var count = 1;
      function increment() {
          ++count;
          if(count &gt; 4)
              count = 1;
          document.getElementById("week").innerHTML = count;
          loadWeek(count);
      }
      function decrement() {
          --count;
          if(count &lt; 1)
              count = 4;
          document.getElementById("week").innerHTML = count;
          loadWeek(count);
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>The php code echoes only, doesn't return...</p>
  
  <pre><code>&lt;?php 
  
  if (isset($_POST['p'])) {
      $week = $_POST['p'];
      week($week);
  }
  
  function week($week) {
      //connects to Database
      //PDO statements
      //php double array
      for ($i = 0; $i &lt; 7; $i++) {
          echo "&lt;tr&gt;&lt;td&gt;$weekDays[$i]&lt;/td&gt;";
          for ($j = 0; $j &lt; 5; $j++) {
              echo "&lt;td&gt;&lt;p&gt;".$usersNames[$i][$j]."&lt;/p&gt;&lt;/td&gt;";
      }
      echo"&lt;/tr&gt;";
  } 
  ?&gt;
  </code></pre>
  
  <p>The above php code used</p>
  
  <pre><code>function loadWeek (x) {
        $.post("weekly.php", {p: x}, function (res) {
           $("body").append(res);
        });
      }
      $(function () {
        loadWeek (1);
  });
  </code></pre>
  
  <p>The php code works grabbing from the database. Like I said the PHP works, it successfully loads up the first week without the javascript manipulation. I don't feel comfortable posting the rest of the php code, especially with the sqli in the code. However, I need the JS to change wha week I need to see. The formatting and how Ajax deals with it when the Ajax is done is my problem.</p>