ajax php传递和接收变量实现思路及代码_PHP教程
程序员文章站
2024-02-14 08:30:10
...
So, your jQuery might be something like.....
$.ajax({
url: 'query.php',
data: {id:10},
datatype: json
success: function(results) {
if (results.msg == 'success') {
for (var i in data) {
$('#content').append(
'id = ' + results.data[i].id + ', description = ' + results.data[i].description + ', msrp = ' + results.data[i].msrp
);
}
} else {
$('#content').append(results.msg);
}
}
});
And your php....
if (isset($_GET['id'])) {
$sql = "SELECT id, description, msrp FROM tbl WHERE id = '{$_GET['id']}'";
$return = array();
if ($result = mysql_query($sql)) {
if (mysql_num_rows($result)) {
$return['msg'] = 'success';
while ($row = mysql_fetch_assoc($result)) {
$return['data'][] = $row;
}
} else {
$return['msg'] = 'No results found';
} else {
$return['msg'] = 'Query failed';
}
header("Content-type: application/json");
echo json_encode($result);
}
复制代码 代码如下:
$.ajax({
url: 'query.php',
data: {id:10},
datatype: json
success: function(results) {
if (results.msg == 'success') {
for (var i in data) {
$('#content').append(
'id = ' + results.data[i].id + ', description = ' + results.data[i].description + ', msrp = ' + results.data[i].msrp
);
}
} else {
$('#content').append(results.msg);
}
}
});
And your php....
复制代码 代码如下:
if (isset($_GET['id'])) {
$sql = "SELECT id, description, msrp FROM tbl WHERE id = '{$_GET['id']}'";
$return = array();
if ($result = mysql_query($sql)) {
if (mysql_num_rows($result)) {
$return['msg'] = 'success';
while ($row = mysql_fetch_assoc($result)) {
$return['data'][] = $row;
}
} else {
$return['msg'] = 'No results found';
} else {
$return['msg'] = 'Query failed';
}
header("Content-type: application/json");
echo json_encode($result);
}
上一篇: mysql分布式中间件cobar
推荐阅读
-
ajax php传递和接收变量实现思路及代码_PHP教程
-
php和javascript之间变量的传递实现代码_PHP教程
-
php格式化时间戳显示友好的时间实现思路及代码_PHP教程
-
php格式化时间戳显示友好的时间实现思路及代码,php思路_PHP教程
-
ajax php传递和接收变量实现思路及代码
-
php格式化时间戳显示友好的时间实现思路及代码_PHP教程
-
php和javascript之间变量的传递实现代码
-
ajax php传递和接收变量实现思路及代码
-
PHP利用MySQL保存session的实现思路及示例代码,mysqlsession_PHP教程
-
使用php方法curl抓取AJAX异步内容思路分析及代码分享,curlajax_PHP教程