dsxfa26482 2016-03-27 09:42
浏览 40

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

i was trying to get data from my database using jquery ajax . this is the code:

<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>

but i didnt got the data . it shows a empty page. the php code which fetches the data is :

<?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>";
?>

i dont know why its not showing any data when my 'tblsample' table in my database 'mydbrun' has 2 entries.i want to display my output in a div element whose id is 'livedata'. I am using html5 attribute contenteditable , is it causing a problem ? i have copied this code from somewhere and i dont know what the attribute 'data-id1' ,'data-id2' mean . plz help . thanks in advance :) .

  • 写回答

1条回答 默认 最新

  • duanqinqian5299 2016-03-27 10:13
    关注

    your url is not returning anything..

     print_r(json_encode($output)) 
    

    at the end .of your php file.

    and add dataType:'json' in your ajax.

    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64