您的位置 >>> 星想互联 >>> 编程技术 >>> PHP高级编程
使用AJAX实现数据库交互
点击数:2282  发布时间2018-04-24 09:27:54

index.php页面:


<script src="jquery-1.7.1.min.js"></script>
<script>
$(function(){
$('#btn').click(function(){
$.get('show.php',{'id':2},function(data){

var json = JSON.parse(data);
for(i in json){
document.write(json[i]['name']);
}
//console.log(json[0]['name'])
//document.write(data);
})
})
})
</script>

show.php页面:

<?php

$pdo=new pdo('mysql:host=localhost;dbname=stu','root','');
$pdo->query('set names utf8');

$sql="selectxxx * from xs";
$stmt=$pdo->query($sql);
$rs=$stmt->fetchAll();

$json = json_encode($rs);

echo ($json);

?>

来源:咸宁网站建设