首頁  >  文章  >  後端開發  >  ajax與php傳遞和接收變數的實作程式碼

ajax與php傳遞和接收變數的實作程式碼

WBOY
WBOY原創
2016-07-25 09:03:48850瀏覽
  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