0

Main issue: 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 <tbody> tags.

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?

The following code is in the webpage I am loading up.

     <tbody>
        <script>
            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
        </script>
    </tbody>

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?

EDIT: words

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.

This script is in the bottom of the code.

<script>
    var count = 1;
    function increment() {
        ++count;
        if(count > 4)
            count = 1;
        document.getElementById("week").innerHTML = count;
        loadWeek(count);
    }
    function decrement() {
        --count;
        if(count < 1)
            count = 4;
        document.getElementById("week").innerHTML = count;
        loadWeek(count);
    }
</script>

The php code echoes only, doesn't return...

<?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 < 7; $i++) {
        echo "<tr><td>$weekDays[$i]</td>";
        for ($j = 0; $j < 5; $j++) {
            echo "<td><p>".$usersNames[$i][$j]."</p></td>";
    }
    echo"</tr>";
} 
?>

The above php code used

function loadWeek (x) {
      $.post("weekly.php", {p: x}, function (res) {
         $("body").append(res);
      });
    }
    $(function () {
      loadWeek (1);
});

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.

  • It rather depends upon what you are sending from the PHP so show us that code also – RiggsFolly Dec 10 '15 at 11:36
  • *Did you even see my answer? Don't you know how to add code in the post? Seriously? Did you read how to post?* – Praveen Kumar Purushothaman Dec 10 '15 at 11:45
  • @PraveenKumar Sorry for being a slow typer. I added your code but now the html page doesn't even load the first week when loaded in. The buttons now work, but only load week 1. Lastly, the schedule is being echoed on the bottom of the page still, not between the <"tbody"> tags. – user3920645 Dec 10 '15 at 12:03
  • @user3920645 Please post the PHP code, what exactly it takes in? – Praveen Kumar Purushothaman Dec 10 '15 at 12:04
  • @PraveenKumar Oh sorry when I updated the post I forgot to update what code I used. Yes I used your code – user3920645 Dec 10 '15 at 12:19
  • @user3920645 Now what's happening? – Praveen Kumar Purushothaman Dec 10 '15 at 12:19
  • @PraveenKumar The schedule from the php is still being echoed on the bottom of the page and not between the <"tbody"> tags where the Ajax scrippt is located. – user3920645 Dec 10 '15 at 12:23
  • @PraveenKumar It works, but now it adds the next week to the viewer instead of updating the week. Do I have to do some page refreshing method? Also, my buttons which pass the week number to loadweek() are not printing that week, except the first week. – user3920645 Dec 10 '15 at 12:32
  • @user3920645 No page refreshing. You need to figure out how to send right week numbers to the function. – Praveen Kumar Purushothaman Dec 10 '15 at 12:32
  • @PraveenKumar You misunderstood. Where each week contains 7 days (rows echoed), it echoes out another 7 rows. So for how many n weeks you view, the webpage displays n weeks worth of schedules. I just want to see one week at a time. – user3920645 Dec 10 '15 at 12:36
  • @PraveenKumar Would you kindly let me know what the question is that I should ask? I'm like a boy that can differentiate between an apple and an orange to colors. While you seem to understand much more like where they grow, what nutrients they contain, and more. Of course I will reward you with the point for answering the question. – user3920645 Dec 10 '15 at 12:44
  • @user3920645 Ha ha... Ask a question man. I will correct it with your further inputs. – Praveen Kumar Purushothaman Dec 10 '15 at 12:45
  • @PraveenKumar That was my question... How do I dynamically display one week's worth of schedule without simply appending each week? – user3920645 Dec 10 '15 at 12:49
  • @user3920645 You need to fetch the whole week's schedules by changing your SQL. – Praveen Kumar Purushothaman Dec 10 '15 at 12:50
  • @PraveenKumar Still that doesn't help with why I get 14 rows or 2 seven day duplicates in between my tbody. Do I delete the html between the tbody then append the next week? Also, my php works and correctly selects nth week being passed, but theres an issue with Ajax. – user3920645 Dec 10 '15 at 12:58
  • @user3920645 Okay, do this. Create a question with the Database model you have, along with the SQL query you are putting, the PHP code and the AJAX Code. Post the link here, may be. I can have a look into it and guide you. It is totally irrelevant for this question. `:)` – Praveen Kumar Purushothaman Dec 10 '15 at 13:01
  • @PraveenKumar Disregard the dynamic weekly viewer. Can I update html as opposed to appending html and having an infinitely long page that adds each week? – user3920645 Dec 10 '15 at 13:09
  • @user3920645 That's bad. – Praveen Kumar Purushothaman Dec 10 '15 at 13:09
  • @PraveenKumar You misunderstood. Can I update html as opposed to both (appending html and having an infinitely long page that adds each week)? That's bad doesn't help. – user3920645 Dec 10 '15 at 13:16

1 Answers1

1

You are confusing everything. Let me clear your scripts...

function loadWeek (x) {
  $.post("weekly.php", {p: x}, function (res) {
     $("tbody").append(res);
  });
}
$(function () {
  loadWeek (1);
});

The PHP code now gets:

$_POST["p"] => x

In your first code, you were just sending:

weekly.php?1            // This is not the valid request I believe.

Now my code changes it to:

weekly.php?p=1 // You need to change the `param_name` to whatever here.
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252