ajax php传递和接收变量实现思路及代码

编程学习 2021-07-05 09:49www.dzhlxh.cn编程入门
在实际应用中会遇到这种情况:ajax与php之间传递和接收变量,为了实现这一功能应用,很多新手朋友们,只能望洋兴叹啊,于是本人搜集整理一些实用技巧,晒出来和大家分享,希望可以帮助你们
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);
}

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板