首页 >后端开发 >php教程 >ajax与php传递和接收变量的实现代码

ajax与php传递和接收变量的实现代码

WBOY
WBOY原创
2016-07-25 09:03:48912浏览
  1. $.ajax({
  2. url: 'query.php',
  3. data: {id:10},
  4. datatype: json
  5. success: function(results) {
  6. if (results.msg == 'success') {
  7. for (var i in data) {
  8. $('#content').append(
  9. 'id = ' + results.data[i].id + ', description = ' + results.data[i].description + ', msrp = ' + results.data[i].msrp
  10. );
  11. }
  12. } else {
  13. $('#content').append(results.msg);
  14. }
  15. }
  16. });
复制代码

php代码:

  1. if (isset($_GET['id'])) {
  2. $sql = "SELECT id, description, msrp FROM tbl WHERE id = '{$_GET['id']}'";
  3. $return = array();
  4. if ($result = mysql_query($sql)) {
  5. if (mysql_num_rows($result)) {
  6. $return['msg'] = 'success';
  7. while ($row = mysql_fetch_assoc($result)) {
  8. $return['data'][] = $row;
  9. }
  10. } else {
  11. $return['msg'] = 'No results found';
  12. } else {
  13. $return['msg'] = 'Query failed';
  14. }
  15. header("Content-type: application/json");
  16. echo json_encode($result);
  17. }
复制代码


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn