日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

ajax怎么在html与php中使用,如何使用ajax和php将数据从数据库表放到html表

發布時間:2023/12/19 php 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。