Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我只是想把AJAX应用到我的tbody上,而不是所有的表上_Php_Ajax - Fatal编程技术网

Php 我只是想把AJAX应用到我的tbody上,而不是所有的表上

Php 我只是想把AJAX应用到我的tbody上,而不是所有的表上,php,ajax,Php,Ajax,我试图将Ajax应用到我的tbody上,但不是所有的表,但我不能这样做。如果我将完整的表放在页面中,AJAX工作得很好。我想将我的表头和表体拆分为单独的页面。以下代码是AJAX: function jsQueryData() { var objData = { action: actionSplit0 + '/QueryData', }; $.post( index_File, objData, function(data, status) {

我试图将Ajax应用到我的tbody上,但不是所有的表,但我不能这样做。如果我将完整的表放在页面中,AJAX工作得很好。我想将我的表头和表体拆分为单独的页面。以下代码是AJAX:

function jsQueryData() {
var objData = {
    action: actionSplit0 + '/QueryData',
};

$.post(
    index_File,
    objData,
    function(data, status) {
        $('#table-container').html(data)
    },
    'html'
)
}

这是我桌子的代码。如果我将表代码完全放在一个页面中,AJAX将很好地工作:

    <table class="table table-striped table-bordered table-hover 
       main-table-h">
      <thead>
        <tr>
          <td scope="col" class="search-th">
            <input type="text" name="" value=""> <!-- for search -->
          </td>
        </tr>
        <tr>
          <th scope="col" class="align-top">item no</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($allData1 as $data): ?>
          <tr>
            <td><?=$data->{2}; ?></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
我曾尝试将上述代码拆分为单独的页面中的tbody,但数据并不是以表格的形式提供的,而是所有的tds一起提供的


您需要将ajax结果作为json返回。 我没有看到您的表和数据库结构,所以让我按如下方式创建一个表,并将值插入其中 测试方法

CREATE TABLE `users` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
插入测试声明

INSERT INTO `users` (`id`, `username`, `name`, `email`, `timestamp`) VALUES
(1, 'nancy1', 'Nancy moore  ', 'nancyfake@gmail.com', '2018-11-16 05:02:35'),
(2, 'tony1', 'tony moore    ', 'tonyfake@gmail.com', '2018-11-16 05:08:23');
下面是对index.php的重写,它反映了ajax/jquery代码

<!doctype html>
<html>
    <head>
        <title></title>

       <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script type="text/javascript">
$(document).ready(function(){
    $.ajax({
        url: 'result.php',
        type: 'get',
        dataType: 'JSON',
        success: function(response){
            var len = response.length;
            for(var i=0; i<len; i++){
                var id = response[i].id;
                var username = response[i].username;
                var name = response[i].name;
                var email = response[i].email;

                var tr_str = "<tr>" +
                    "<td align='center'>" + (i+1) + "</td>" +
                    "<td align='center'>" + username + "</td>" +
                    "<td align='center'>" + name + "</td>" +
                    "<td align='center'>" + email + "</td>" +
                    "</tr>";

                $("#userTable tbody").append(tr_str);
            }

        }
    });
});
</script>

    </head>
    <body>
        <div class="container">
            <table id="userTable" border="1" >
                <thead>
                    <tr>
                        <th width="5%">S.no</th>
                        <th width="20%">Username</th>
                        <th width="20%">Name</th>
                        <th width="30%">Email</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </body>
</html>
config.php

请记住编辑数据库凭据以适合您的

<?php

$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "your-db-name here"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());
}
result.php


就这些。让我知道进展如何

如果我理解你的问题,你想设置你的thead static,但是你的tbody是用AJAX,如果是的

HTML:

参考库:


如果我误解了你的问题,请让我知道

我可以将JSON数据发送到php变量,然后使用foreach吗?必须为每个变量循环使用。Ajax有一种方法。最好的选择是我为你创建的解决方案。这是您问题的标题,我正在尝试将AJAX应用于我的tbody,而不是所有表。如果你正在寻找每一种方法,你必须重新表述你的问题,看看谁能帮助你,好吗
<?php

include "config.php";

$return_arr = array();

$query = "SELECT * FROM users ORDER BY NAME";

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

while($row = mysqli_fetch_array($result)){
    $id = $row['id'];
    $username = $row['username'];
    $name = $row['name'];
    $email = $row['email'];

    $return_arr[] = array("id" => $id,
                    "username" => $username,
                    "name" => $name,
                    "email" => $email);
}

// Encoding array in JSON format
echo json_encode($return_arr);
 <table class="table table-striped table-bordered table-hover 
   main-table-h">
  <thead>
    <tr>
      <td scope="col" class="search-th">
        <input type="text" name="" value=""> <!-- for search -->
      </td>
    </tr>
    <tr>
      <th scope="col" class="align-top">item no</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>
 $.ajax({
    url: 'result.php',
    type: 'get',
    dataType: 'JSON',
    success: function(response){
        var tableData=$("table").ediTable({
            json:{
                body:response
            },
            editable:false // if you do not want it editable.
        })
        //and if you want to send JSON data of table you can :
        jsonData=tableData.data();
        // now you can send jsonData to PHP via ajax ...
    }
});