首页 > 解决方案 > 在 php 中使用 Ajax 以 Json 数据格式在控制台上显示数据库中的数据记录时出错

问题描述

我正在尝试以 json 格式将数据库记录显示到我的控制台,但我不明白为什么我的 json 格式的请求没有显示在我的控制台上。也没有错误可以指导我在哪里犯了错误。有人能帮我吗。我使用 MVC 框架来编写我的代码。我想根据数据库中的比赛表中的比赛编号检索马名。

这是我的home.php

<div class="box-body">

    Start creating your amazing application! 

    <table class="table table-bordered table-striped dt-responsive tables" width="100%">

      <thead>

       <tr>

         <th style="width:10px">#</th>
         <th>id</th>
         <th>Race Number</th>
         <th>Horse Name</th>
         <th>Actions</th>

       </tr> 

      </thead>

      <tbody>

        <?php

          $users = ControllerUsers::ShowallUsersCtr();

          foreach ($users as $key => $value) {

            echo '

              <tr>
                <td>'.($key+1).'</td>
                <td>'.$value["id"].'</td>
                <td>'.$value["racenumber"].'</td>
                <td>'.$value["horsename"].'</td>';


                echo '<td>

                  <div class="btn-group">

                    <button class= "btn btn-primary btnAddRace" RaceNumber="'.$value["racenumber"].'" data-toggle="modal" data-target="#editUser">Add Horse - Race 1</button>

                  </div>  

                </td>

              </tr>';
          }

        ?>

      </tbody>

    </table>

  </div>

这是我的名为users.js的 javascript 文件代码

$(document).on("click", ".btnAddRace", function(){

var RaceNumber = $(this).attr("RaceNumber"); // capture the race number from database to the console 
console.log("RaceNumber",RaceNumber);



var data = new FormData();
data.append("RaceNumber",RaceNumber);

$.ajax({

    url: "ajax/users.ajax.php",
    method: "POST",
    data: data,
    cache: false,
    contentType: false,
    processData: false,
    dataType: "json",
    success: function(answer){

         console.log("answer", answer); // bring from the database whatever we asked for 


    }

});

});

这是我的 Ajax 文件users.ajax.php

    <?php

require_once "../controllers/users.controller.php";
require_once "../models/users.model.php";

    class AjaxUsers{

        public $RaceNumber;
        public function ajaxEditUser(){

            $item = "racenumber";
            $value = $this->RaceNumber; // value what is selected from the table 

            $answer = ControllerUsers::ctrShowUsers($item, $value); // ask for the controller to show me the race table
            echo json_encode($answer);
        }


    }

    if (isset($_POST["RaceNumber"])) { // if the variable race number comes with information, we can execute all of this

        $edit = new AjaxUsers();
        $edit -> RaceNumber = $_POST["RaceNumber"];
        $edit -> ajaxEditUser();
    }

这是我的用户控制器和模型方法

    static public function ctrShowUsers($item, $value){

    $table = "race";

    $answer = UsersModel::MdlShowUsers($table, $item, $value);
    return $answer;
}
static public function MdlShowUsers($table, $item, $value){

        $stmt = Connection::connect()->prepare("SELECT * FROM $table WHERE $item = :$item");

        $stmt -> bindParam(":".$item, $value, PDO::PARAM_INT);

        $stmt -> execute();

        return $stmt -> fetch();

    $stmt -> close();

    $stmt = null;

}

数据库图片在这里 在这里 输入图片描述

标签: javascriptphpjqueryjsonajax

解决方案


推荐阅读