ajax怎么在html与php中使用,如何使用ajax和php将数据从数据库表放到html表
我想做的是使用ajax和php將數據庫表數據放入索引頁中的html表。
我的問題是數據沒有顯示。有人知道我的代碼有什么問題嗎?
的HTML:
NameAgeGenderAction
腳本:
$(document).ready(function(){
$.ajax({
url: 'process.php',
type: 'post',
data: {tag: 'getData'},
dataType: 'json',
success: function(data){
if(data.success){
$.each(data, function(index, record){
if($.is_numeric(index)){
var row = $("
");$("
").text(record.name).appendTo(row);$("
").text(record.age).appendTo(row);$("
").text(record.gender).appendTo(row);$("
").text(record.action).appendTo(row);row.appendTo('myTable2');
}
})
}
}
});
$('#myTable2').dataTable({
"bjQueryUI": true,
"sPaginationType": "full_numbers"
});
});
process.php
error_reporting(-1);
ini_set('display_errors', 'On');
if(isset($_POST['tag'])){
try{
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";
$dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT name, age, gender, action FROM viewtables";
$result = $dbc->prepare($sql);
if(!$result->execute()) return false;
if($result->rowCount() > 0) {
$json = array();
while ($row = $result->fetch()){
$json[] = array(
'name' => $row['name'],
'age' => $row['age'],
'gender' => $row['gender'],
'action' => $row['action']
);
}
$json['success'] = true;
echo json_encode($json);
}
} catch (PDOException $e) {
echo "Error: " .$e->getMessage();
}
}
?>
總結
以上是生活随笔為你收集整理的ajax怎么在html与php中使用,如何使用ajax和php将数据从数据库表放到html表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为nova9 Pro官方翻新机正式开售
- 下一篇: limesurvey php5.2,li