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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?