通过ajax函数将数据回显到表

时间:2015-08-07 16:16:07

标签: javascript php ajax database

我正在尝试使用ajax函数来回显提交给表的数据。但它并没有像我希望的那样。值变成了一串我不希望发生的值,我希望值以表格形式出现。

以下是我的表单代码

<div id="formbox">
  <form id="formTable">

    <table class="table table-hover">
      <thead>
        <tr>
          <th>Start Time</th>
          <th>End Time</th>
          <th>Monday</th>
          <th>Tuesday</th>
          <th>Wednesday</th>
          <th>Thursday</th>
          <th>Friday</th>
          <th>Saturday</th>
          <th>Sunday</th>
          <th></th>
        </tr>
      </thead>

      <tbody>
        <tr>
          <th>
            <input type="text" class="form-control" name="startTime" id="starttime" width="50px">
          </th>
          <th>
            <input type="text" class="form-control" name="endTime" id="endtime">
          </th>
          <th>
            <input type="checkbox" name="Monday" value="1" id="monday">
          </th>
          <th>
            <input type="checkbox" name="Tuesday" value="1" id="tuesday">
          </th>
          <th>
            <input type="checkbox" name="Wednesday" value="1" id="wednesday">
          </th>
          <th>
            <input type="checkbox" name="Thursday" value="1" id="thursday">
          </th>
          <th>
            <input type="checkbox" name="Friday" value="1" id="friday">
          </th>
          <th>
            <input type="checkbox" name="Saturday" value="1" id="saturday">
          </th>
          <th>
            <input type="checkbox" name="Sunday" value="1" id="sunday">
          </th>
          <th>
            <input type="button" class="btn btn-success" value="Add" id="send">
          </th>
        </tr>
      </tbody>
    </table>

  </form>
</div>
<table class="table table-hover">
  <thead>
    <tr>
      <th>Start Time</th>
      <th>End Time</th>
      <th>Monday</th>
      <th>Tuesday</th>
      <th>Wednesday</th>
      <th>Thursday</th>
      <th>Friday</th>
      <th>Saturday</th>
      <th>Sunday</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <div id="comment"></div>
  </tbody>
</table>

我的javascript代码,

  $(document).ready(function() {

    function showComment() {
      $.ajax({
        type: "post",
        url: "registerarray.php",
        data: "action=showcomment",
        success: function(data) {
          $("#comment").html(data);
        }
      });
    }
    showComment();

    $("#send").click(function() {

      var starttime = $("#starttime").val();
      var endtime = $("#endtime").val();

      if (document.getElementById('monday').checked) {
        var monday = $("#monday").val();
      }
      if (document.getElementById('tuesday').checked) {
        var tuesday = $("#tuesday").val();
      }
      if (document.getElementById('wednesday').checked) {
        var wednesday = $("#wednesday").val();
      }
      if (document.getElementById('thursday').checked) {
        var thursday = $("#thursday").val();
      }
      if (document.getElementById('friday').checked) {
        var friday = $("#friday").val();
      }
      if (document.getElementById('saturday').checked) {
        var saturday = $("#saturday").val();
      }
      if (document.getElementById('sunday').checked) {
        var sunday = $("#sunday").val();
      }



      var dataString = "starttime1=" + starttime + "&endtime1=" + endtime + "&monday1=" + monday + "&tuesday1=" + tuesday + "&wednesday1=" + wednesday + "&thursday1=" + thursday + "&friday1=" + friday + "&saturday1=" + saturday + "&sunday1=" + sunday + "&action=addcomment";

      $.ajax({
        type: "post",
        url: "registerarray.php",
        data: dataString,
        success: function(data) {
          showComment();
          $("#formTable")[0].reset();

        }

      });

    });
  });

我的寄存器数组代码,用于注册和回显数据库中的数据,

<?php

$action = $_POST["action"];

if ($action == "showcomment") {

    require_once 'dbfunction.php';
    $con = getDbConnect();

    if (!mysqli_connect_errno($con)) {
        $show = "SELECT * FROM scheduletime";

        $result = mysqli_query($con, $show);

        while ($row = mysqli_fetch_array($result)) {
            echo "<tr>";
            echo "<th>" . $row["startTime"] . "</th>";
            echo "<th>" . $row["endTime"] . "</th>";
            echo "<th>" . $row["Monday"] . "</th>";
            echo "<th>" . $row["Tuesday"] . "</th>";
            echo "<th>" . $row["Wednesday"] . "</th>";
            echo "<th>" . $row["Thursday"] . "</th>";
            echo "<th>" . $row["Friday"] . "</th>";
            echo "<th>" . $row["Saturday"] . "</th>";
            echo "<th>" . $row["Sunday"] . "</th>";
            echo "<th></th>";
            echo "</tr>";
        }

        mysqli_close($con);
    } else {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    ?>


    <?php

} else if ($action == "addcomment") {
    require_once 'dbfunction.php';
    $con = getDbConnect();

    $starttime2 = $_POST['starttime1'];
    $endtime2 = $_POST['endtime1'];
    $monday2 = $_POST['monday1'];
    $tuesday2 = $_POST['tuesday1'];
    $wednesday2 = $_POST['wednesday1'];
    $thursday2 = $_POST['thursday1'];
    $friday2 = $_POST['friday1'];
    $saturday2 = $_POST['saturday1'];
    $sunday2 = $_POST['sunday1'];

    if (mysqli_connect_errno($con)) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    } else {
        $query = "INSERT INTO scheduletime (startTime, endTime, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)" .
                "VALUES ('$starttime2', '$endtime2', '$monday2', '$tuesday2', '$wednesday2', '$thursday2', '$friday2', '$saturday2', '$sunday2')";

        mysqli_query($con, $query);
    }
    echo "Your comment has been sent";
    mysqli_close($con);
}

我收到的当前输出是一长串数据链接在一起而不是归入表中。

我希望相应地将值字符串插入表中。

2 个答案:

答案 0 :(得分:1)

您需要创建一个包含您尝试传递的html表的字符串,而不是在while循环的每次迭代中回显您的结果。为此,请更改此:

   while ($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<th>" . $row["startTime"] . "</th>";
        echo "<th>" . $row["endTime"] . "</th>";
        echo "<th>" . $row["Monday"] . "</th>";
        echo "<th>" . $row["Tuesday"] . "</th>";
        echo "<th>" . $row["Wednesday"] . "</th>";
        echo "<th>" . $row["Thursday"] . "</th>";
        echo "<th>" . $row["Friday"] . "</th>";
        echo "<th>" . $row["Saturday"] . "</th>";
        echo "<th>" . $row["Sunday"] . "</th>";
        echo "<th></th>";
        echo "</tr>";
    }

到此:

   $html = "";
   while ($row = mysqli_fetch_array($result)) {
        $r =  "<tr>";
        $r .=  "<td>" . $row["startTime"] . "</td>";
        $r .=  "<td>" . $row["endTime"] . "</td>";
        $r .=  "<td>" . $row["Monday"] . "</td>";
        $r .=  "<td>" . $row["Tuesday"] . "</td>";
        $r .=  "<td>" . $row["Wednesday"] . "</td>";
        $r .=  "<td>" . $row["Thursday"] . "</td>";
        $r .=  "<td>" . $row["Friday"] . "</td>";
        $r .=  "<td>" . $row["Saturday"] . "</td>";
        $r .=  "<td>" . $row["Sunday"] . "</td>";
        $r .=  "<td></td>";
        $r .=  "</tr>";
        $html .= $r;
    }
    echo $html;

<强>更新

我现在看到您提供了显示#comment的代码。问题是您在div内使用table您需要对其进行更改,因为除非div位于div,否则您的浏览器会将任何<td>放在表格之外{1}}标签。删除该div,然后将ID添加到tbody

改变这个:

<tbody>
  <div id="comment"></div>
</tbody>

到此:

<tbody id="comment">
</tbody>

答案 1 :(得分:0)

尝试显式定义您期望从服务器获取的dataType:

dataType: "html"

代码将是:

function showComment() {
      $.ajax({
        type: "post",
        url: "registerarray.php",
        data: "action=showcomment",
        dataType: "html",
        success: function(data) {
          $("#comment").html(data);
        }
});