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.