gpt4 book ai didi

php - 使用 AJAX 和 PHP 检索数据

转载 作者:行者123 更新时间:2023-11-29 23:23:38 26 4
gpt4 key购买 nike

我正在尝试调用 mysql 服务器来检索几个产品并将它们转换为表,但它似乎根本不起作用。

我刚刚学习服务器端,所以任何解释都会很棒,谢谢大家!

列表.php:

<?php
$pdo = new PDO('mysql:localhost:8888;dbname=searchable-db', 'pim-admin', 'admin');
$select = 'SELECT *';
$from = ' FROM `products`';
$where = ' WHERE TRUE';
$sql = $select . $from . $where;
$statement = $pdo->prepare($sql);
$statement->execute*();
$results=$statement->fetchAll(PD0::FETCH_ASSOC);
$json=json_encode($results);
echo($json);
?>

HTML:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>AJAX Filtering</title>
<link rel="stylesheet" type="text/css" href="styles/styles.css">
</head>


<body>
<h1>Product Database</h1>
<div id="products"></div>

<table id="pieces">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Company</th>
<th>Material</th>
<th>Category</th>
<th>Style</th>
<th>Color</th>
</tr>
</thead>
</tbody>
</tbody>
</table>

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

<script>
function updateProducts(){
$.ajax({
type: "POST",
url: "list.php",
dataTyoe : 'json',
cache: false,
success: function(records){
$('#products').text(JSON.stringify(records, null, 4));
}
});
}

updateProducts();

function makeTable(data){
var tbl_body = "";
$.each(data, function() {
var tbl_row = "";
$.each(this, function(k, v) {
tbl_row += "<td>"+v+"</td>";
})
tbl_body += "<tr>"+tbl_row+"</tr>";
})
return tbl_body;
}

$('#pieces tbody').html(makeTable(records));
</script>
</body>
</html>

@SmileOff,这就是我现在所拥有的,没有错误,但仍然无法调用我的数据:/

<script>
function updateProducts(){
$.ajax({
type: "POST",
url: "includes/list.php",
success: function(records){
console.log($.parseJSON(records));
}
});

updateProducts();

function makeTable(data){
var tbl_body = "";
$.each(data, function() {
var tbl_row = "";
$.each(this, function(k, v) {
tbl_row += "<td>"+v+"</td>";
})
tbl_body += "<tr>"+tbl_row+"</tr>";
})
return tbl_body;
}

$('#pieces tbody').html(makeTable('records'));
</script>

==================@SmileOff

JS

function updateProducts(){
$.ajax({
type: "POST",
url: "includes/list.php",
success: function(){
console.log($.parseJSON(records));
}
});
}

updateProducts();

function makeTable(data){
var tbl_body = "";
$.each(data, function() {
var tbl_row = "";
$.each(this, function(k, v) {
tbl_row += "<td>"+v+"</td>";
})
tbl_body += "<tr>"+tbl_row+"</tr>";
})
return tbl_body;
}
$('#pieces tbody').html(makeTable('records'));

列表.php

<?php
$pdo = new PDO('mysql:host=localhost:8888;dbname=searchable-db', 'pim-admin', 'admin');
$select = 'SELECT *';
$from = ' FROM products';
$where = ' WHERE TRUE';
$sql = $select . $from . $where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results=$statement->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($results);
echo($json);
?>

最佳答案

首先改变这个:dataTyoe : 'json' - 没有 dataTyoe,只有 dataTypecache: false - 这是默认值,您不需要再次设置

$(function(){


function updateProducts(){
$.ajax({
type: "POST",
url: "testData.php",
success: function(records){
console.log($.parseJSON(records));
makeTable($.parseJSON(records));

}
});
}

updateProducts();
function makeTable(data){
console.log(data);
//update your html or something else
}

});

在你的 php 文件中写下这个,仅供测试

<?php


echo json_encode(array(
array(
"id" => 2,
"title" => "MVC architecture in JS",
"price" => 89.00,
"cover" => "mvc.jpg"
),
array(
"id" => 3,
"title" => "JavaScript - Good Parts",
"price" => 99.99,
"cover" => "good-parts.jpg"
),
array(
"id" => 4,
"title" => "JavaScript Design Patterns",
"price" => 149.50,
"cover" => "patterns.jpg"
),
array(
"id" => 5,
"title" => "3D World with Three.js",
"price" => 299.00,
"cover" => "three.jpg"
)
));



?>

关于php - 使用 AJAX 和 PHP 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27113919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com