[英]jQuery ajax request for data from a database not working
i was trying to get data from my database using jquery ajax .我试图使用 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代码是:
<?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'.当我的数据库“mydbrun”中的“tblsample”表有 2 个条目时,我不知道为什么它不显示任何数据。我想在 id 为“livedata”的 div 元素中显示我的输出。 I am using html5 attribute contenteditable , is it causing a problem ?我正在使用 html5 属性 contenteditable ,它会导致问题吗? i have copied this code from somewhere and i dont know what the attribute 'data-id1' ,'data-id2' mean .我从某处复制了这段代码,但我不知道属性 'data-id1' 、'data-id2' 是什么意思。 plz help .请帮忙。 thanks in advance :) .提前致谢 :) 。
your url is not returning anything..你的网址没有返回任何东西..
print_r(json_encode($output))
at the end .of your php file.在 .of php 文件的末尾。
and add dataType:'json'
in your ajax.并在您的 ajax 中添加dataType:'json'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.