【问题标题】:Actions links not working while fetching data from database using ajax in php在 php 中使用 ajax 从数据库中获取数据时,操作链接不起作用
【发布时间】:2020-09-28 07:10:21
【问题描述】:

附加了错误输出的屏幕截图。操作链接不起作用。]2我正在使用 ajax 从 php 中的 mysql 数据库中获取数据并使用返回的数据填充表。 HTML 表格正在填充数据,但操作链接(编辑、查看和删除)不起作用。

index.php

<div id="employee_table">  
                          <table class="table table-bordered text-center">  
                            <thead class="bg-dark text-center text-white">
                               <tr>  
                                    <th>Employee Name</th> 
                                    <th>Address</th>         
                                    <th>Gender</th> 
                                    <th>Designation</th>         
                                    <th>Age</th>         
                                    <th>Photo</th>                                                  
                                    <th>Edit</th>  
                                    <th>View</th>  
                                    <th>Delete</th>  
                               </tr>
                            </thead>
                            <tbody id="tableBody">                          
                               <?php  
                               
                               $query = "SELECT * FROM tbl_employee ORDER BY name";  
                               $result = mysqli_query($connect, $query);
                               while($row = mysqli_fetch_array($result))  
                               {  
                               ?>  
                               <tr>  
                                    <td><?php echo $row["name"]; ?></td>  
                                    <td><?php echo $row["address"]; ?></td>  
                                    <td><?php echo $row["gender"]; ?></td>  
                                    <td><?php echo $row["designation"]; ?></td>  
                                    <td><?php echo $row["age"]; ?></td>
                                    <td><img src='<?php echo $row["image"]; ?>' width=80 height=30/></td>    
                                    <td><a  id="<?php echo $row["id"]; ?>" class="btn btn-warning btn-sm edit_data"/><i class="fa fa-pencil-square" aria-hidden="true"></i></a></td>  
                                    <td><a  id="<?php echo $row["id"]; ?>" class="btn btn-info btn-sm view_data"/><i class="fa fa-eye" aria-hidden="true"></i></a></td>  
                                    <td><a  id="<?php echo $row["id"]; ?>" class="btn btn-danger btn-sm  del_data"/><i class='fa fa-trash' aria-hidden='true'></i></a></td>  
                               </tr>  
                               <?php  
                               }  
                               ?>
                           <tbody>                             
                          </table>  
                     </div>  

此时,编辑、查看和删除链接正在工作。但是在插入、更新或删除数据时,返回的链接不起作用。

scripts.js

function showUsers(){
           //alert("hello");
         $.ajax({  
                     url:"fetchusers.php",  
                     method:"GET",
                     dataType:"json",  
                     success:function(data){                         
                          $('#tableBody').html(data); 
                     }  
                });  
          }

fetchusers.php

<?php  
 
  require 'db.php';
  $output="";  
  $query = "SELECT * FROM tbl_employee order by name";  
      $result = mysqli_query($connect, $query);  
      //$row = mysqli_fetch_array($result);         
    while($row=mysqli_fetch_array($result)){
      $output .=
                "<tr><td>".
                     $row['name'].
                     "</td> <td>".
                     $row['address'].
                     "</td><td>".
                     $row['gender'].
                     "</td><td>".
                     $row['designation'].
                     "</td><td>".
                     $row['age'].
                     "</td><td><img src=".
                     $row['image'].
                     " width=80 height=30/></td><td><a  id=".
                     $row['id'].
                     "class='btn btn-warning btn-sm edit_data'><i class='fa fa-pencil-square' aria-hidden='true'></i></a></td><td><a  id=".
                     $row['id'].
                     "class='btn btn-info btn-sm view_data'><i class='fa fa-eye' aria-hidden='true'></i></a></td><td><a  id=".
                     $row['id'].
                     "class='btn btn-danger btn-sm del_data'><i class='fa fa-trash' aria-hidden='true'></i></a></td></tr>";
                     
        
    }
    if($output){    
       echo json_encode($output);
    }
    else{
         echo "No data found.";
    }
    
 ?>

此时,编辑、查看和删除链接不起作用。没有事件正在发生。数据正在填充到 html 表中。

【问题讨论】:

  • 成功函数中fetchusers.php返回的输出是什么?你为什么要做 json_encode($output)?我认为应该返回纯字符串,因为代码中没有 JSON 初始化。
  • 即使在删除 json 编码后,表格仍在填充,但操作链接不起作用。
  • 你没有提到 fetchuser 返回的输出。
  • 编辑了问题。附上输出截图。操作链接无效。
  • 编辑了问题。附上输出截图。操作链接无效。

标签: php mysql ajax


【解决方案1】:

$output 格式不正确,请将其更新为以下内容。

$output .=
  "<tr>".
  "<td>".$row['name']."</td>".
  "<td>".$row['address']."</td>".
  "<td>".$row['gender']."</td>".
  "<td>".$row['designation']."</td>".
  "<td>".$row['age']."</td>".
  "<td><img src='".$row['image']."' width=80 height=30/></td>".
  "<td><a id='".$row['id']."' class='btn btn-warning btn-sm edit_data'><i class='fa fa-pencil-square' aria-hidden='true'></i></a></td>".
  "<td><a id='".$row['id']."' class='btn btn-info btn-sm view_data'><i class='fa fa-eye' aria-hidden='true'></i></a></td>".
  "<td><a id='".$row['id']."' class='btn btn-danger btn-sm del_data'><i class='fa fa-trash' aria-hidden='true'></i></a></td>".
  "</tr>";

【讨论】:

    【解决方案2】:

    而不是

    echo json_encode($output);
    

    替换为

    echo $output;
    

    您的 $output 变量不是数组,而是包含 html 的字符串

    【讨论】:

    • 它对输出没有影响。相同的输出。
    猜你喜欢
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 2011-10-21
    • 2015-12-14
    • 2011-03-03
    • 2019-11-26
    • 1970-01-01
    • 2012-09-23
    相关资源
    最近更新 更多