简单的应用程序php ajax无法正常工作

时间:2022-04-26 01:25:33

I made a simple application php & ajax and I don't know why it is not working.

我做了一个简单的应用程序php和ajax,我不知道它为什么不起作用。

My code:

<?php

$grId  = $_GET["groupId"];
$limit = $_GET["limit"];

if ($limit <= 0) {
  $limit = 10;
}

$servername = "localhost";
$username   = "root";
$password   = "root";
$dbname     = "productsdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql  = "SELECT id, displayName, role, groupId FROM user where groupId = ? limit ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $grId, $limit);
$stmt->execute();
$stmt->bind_result($id, $displayName, $role, $groupId);
$users = array();

while($stmt->fetch()) {
  $user              = new StdClass();
  $user->id          = $id;
  $user->displayName = $displayName;
  $user->role        = $role;
  $user->groupId     = $groupId;
  array_push($users, $user);
}

echo json_encode($users);

$stmt->close();
$conn->close();
?>

json-client-get.html:

<html>

<head>
  <script src="jquery-2.1.4.js"></script>
  <script>
    $(document).ready(function() {
      $('#students').hide();
      $("#getStudentsButton").click(function() {
        $.ajax({
          dataType: "json",
          type: "GET",
          url: "getStudentsJson.php",
          data: {
            groupId: 1,
            limit: 7
          },
          success: renderTable
        });
      });

      function renderTable(data) {
        var trHTML = '';
        $.each(data, function(i, item) {
          trHTML += '<tr><td>' + item.id + '</td><td>' + item.displayName + '</td><td>' + item.role + '</td> <td> ' + item.groupId + ' </td></tr>';
        });

        $('#students').append(trHTML);
        $('#students').show();
      }

    });
  </script>
</head>

<body>

  Lista studentilor din grupul 1:
  <div id="maindiv">
    <table id="students" border="1">
      <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Role</th>
        <th>GroupId</th>
      </tr>
    </table>
  </div>
  <input id="getStudentsButton" type="button" value="Load students" />
</body>
</html>

I craete in phpmyadmin a new database productsdb with the table user and a single value inserted.

我在phpmyadmin中创建了一个新的数据库productsdb,其中包含表用户并插入了一个值。

When I run localhost/folder/json-client-get.html and press the button Load student nothing is happening.

当我运行localhost / folder / json-client-get.html并按下按钮Load学生没有任何事情发生。

EDIT(picture with my db but I don't know why the photo is not working)

编辑(图片与我的数据库,但我不知道为什么照片不工作)

简单的应用程序php ajax无法正常工作

2 个解决方案

#1


1  

I tested your code and there is an issue with SQL which you have:

我测试了你的代码,你有一个SQL问题:

$sql = "SELECT id, displayName, role, groupId FROM user where groupId = ? limit ?";

check table structure and ensure which you have these fields

检查表结构并确保您拥有这些字段

for sure run this query in your phpmyadmin and see result

确保在phpmyadmin中运行此查询并查看结果

SELECT id, displayName, role, groupId FROM user where groupId = 1 limit 7

#2


0  

This answer will not solve your issue but simply show you how to debug your ajax call. I think you will benefit more from knowing how to solve it than getting it solved.

这个答案不会解决您的问题,只是告诉您如何调试您的ajax调用。我认为你会从知道如何解决它而不是解决它中获益更多。

Take a look at the following ajax call:

看看下面的ajax调用:

$(document).ready(function(e) {
    $.ajax({
        url: 'ajaxScript.php',
        type: 'POST',
        data: {
            'somevar': 'somevalue'
        },
        success: function(result)
        {
            alert('Successfully called');
        },
        error: function(exception)
        {
            alert('Exeption:'+exception);
        }
    })
})

#1


1  

I tested your code and there is an issue with SQL which you have:

我测试了你的代码,你有一个SQL问题:

$sql = "SELECT id, displayName, role, groupId FROM user where groupId = ? limit ?";

check table structure and ensure which you have these fields

检查表结构并确保您拥有这些字段

for sure run this query in your phpmyadmin and see result

确保在phpmyadmin中运行此查询并查看结果

SELECT id, displayName, role, groupId FROM user where groupId = 1 limit 7

#2


0  

This answer will not solve your issue but simply show you how to debug your ajax call. I think you will benefit more from knowing how to solve it than getting it solved.

这个答案不会解决您的问题,只是告诉您如何调试您的ajax调用。我认为你会从知道如何解决它而不是解决它中获益更多。

Take a look at the following ajax call:

看看下面的ajax调用:

$(document).ready(function(e) {
    $.ajax({
        url: 'ajaxScript.php',
        type: 'POST',
        data: {
            'somevar': 'somevalue'
        },
        success: function(result)
        {
            alert('Successfully called');
        },
        error: function(exception)
        {
            alert('Exeption:'+exception);
        }
    })
})