繁体   English   中英

来自数据库的数据的jQuery ajax请求不起作用

[英]jQuery ajax request for data from a database not working

我试图使用 jquery ajax 从我的数据库中获取数据。 这是代码:

<script>
$(document).ready(function(){
function fetch_data(){
$.ajax({
type:"POST",
url:"http://localhost:88/phpPoint/select.php",
success:function(response){$("#livedata").html(response);}
});
}
fetch_data();
/*$(document).on("click","#btnadd",function(){
    var firstname=$("#firstname").text();
    var lastname=$("#lastname").text();
    if(firstname==''){
        alert("enter first name");
        return false;
    }
    if(lastname==''){
        alert("enter last name");
        return false;
    }
    $.ajax({
        type:"post",
        url:"insert.php",
        data:{firstname:firstname,lastname:lastname},
        dataType:"text",
        success:function(data){alert(data);
        fetch_data();}

    });
});*/
});
</script>

但我没有得到数据。 它显示一个空白页面。 获取数据的php代码是:

<?php
$connect=mysqli_connect("localhost","root","***********","mydbrun");
$output=$row="";
$sql="SELECT * FROM tblsample ORDER BY id DESC";
$result=mysqli_query($connect,$sql);
$output.="<div class='table-responsive'>
        <table class='table table-bordered'>
        <tr>
        <th style='width:10%'>Id</th>
        <th style='width:40%'>Firstname</th>
        <th style='width:40%'>Lastname</th>
        <th style='width:10%'>Delete</th>
        </tr>";
        if(mysqli_num_rows($result)>0){
            while($row=mysqli_fetch_array($result))
            {
                $output.="<td>".$row['id']."</td>
                <td class='firstname' data-id1='".$row['id']."' contenteditable>".$row['firstname']."</td>
                <td class='lastname' data-id2='".$row['id']."' contenteditable>".$row['lastname']."</td>
                <td><button name='btndelete' id='btndelete' data-id3='".$row['id']."'>x</button></td>";
            }
            $output.="<tr>
                      <td></td>
                     <td id='firstname' contenteditable></td>
                     <td id='lastname' contenteditable></td>
        <td><button id='btnadd' name='btnadd' class='btn btn-success'>+</button></td></tr>"; }
        else{
        $output.="<tr><td colspan='4'>Data Not Found</td></tr>";    
        }
$output.="</table>
 </div>";
?>

当我的数据库“mydbrun”中的“tblsample”表有 2 个条目时,我不知道为什么它不显示任何数据。我想在 id 为“livedata”的 div 元素中显示我的输出。 我正在使用 html5 属性 contenteditable ,它会导致问题吗? 我从某处复制了这段代码,但我不知道属性 'data-id1' 、'data-id2' 是什么意思。 请帮忙。 提前致谢 :) 。

你的网址没有返回任何东西..

 print_r(json_encode($output)) 

在 .of php 文件的末尾。

并在您的 ajax 中添加dataType:'json'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM