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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?