dounianxie2058 2015-12-10 11:34
浏览 29
已采纳

Ajax响应PHP回显HTML

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.

  • 写回答

1条回答 默认 最新

  • dongzhao1930 2015-12-10 11:36
    关注

    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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)