ajax php传递和接收变量实现思路及代码
程序员文章站
2022-04-13 11:00:13
so, your jquery might be something like..... 复制代码 代码如下: $.ajax({ url: 'query.php', dat...
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);
}
上一篇: 因为是亲生的
下一篇: 意不意外?刺不刺激?惊不惊喜?