Home >Backend Development >PHP Tutorial >Ajax and php implementation code for passing and receiving variables

Ajax and php implementation code for passing and receiving variables

WBOY
WBOYOriginal
2016-07-25 09:03:48926browse
  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. }
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn