【问题标题】:data output from database through ajax jquery通过ajax jquery从数据库输出数据
【发布时间】:2019-11-29 17:48:49
【问题描述】:

当我单击每个按钮的 ID 时,如何从我的数据库中获取数据 我的控制器功能

public function ajax (){
   $resultdata = $this->Booking_model->roominfo();
   echo json_encode($resultdata);
}

这是我的按钮及其数据室 ID 和类

<button class="bkng bkn-room trans_200" data-room-id ="1">BOOK NOW</button>

这是我的模型

public function roominfo() {
  //if(isset($_POST["id"]))
  //{
        $sql = "Select name, price from rooms WHERE room_id = 'id'";
        $result =  $this->db->query($sql);
        return $result->result_array();
  //}

我的 ajax jquery 及其按钮类和数据室 ID

$(document).ready(function() {
        $(".bkng").on("click", function(event) {
              event.preventDefault(); 
            var id = $(this).data('room-id');
                console.log(id); 
                if(id != '') 
                {
                    $.ajax( {
                    type:"get",
                    url : "Pages/ajax",
                    data:{id:id}, 
                    success:function(data)
                    {
                        alert(data);
                        result = JSON.parse(data); 
                        $('#test1').append(result[0]['name']);
                        $("#test2").append(result[0]['price']);
                        $("#test3").append(result[0]['price']);
                        console.log(data); 
                    }
                });
            } 
            });
        }); 

它总是在控制台错误中说 Uncaught TypeError: Cannot read property 'name' of undefined

【问题讨论】:

  • console.log(result) 包含名称属性?
  • @Jed 您的查询是否返回name 属性?
  • 不,它不返回 name 属性它无法读取 [0] 和 name 属性我不知道该怎么做

标签: php jquery ajax codeigniter


【解决方案1】:

您收到此错误,因为您的查询没有返回任何内容,因为您可能没有id = 'id' 的房间。因此 JSON.parse 返回此错误。

所以首先你需要修复你的模型构建一个有效的查询,比如:

function roominfo() {
    $id=$_POST["id"];
    $query=$this->db->select('name, price')
                    ->where('room_id', $id)
                    ->get('rooms');
    return ($query->num_rows())? $query->result_array():false;  
}   

然后在您的 ajax 成功时,您可以检查返回的数据: console.dir(data) 而不是 alert 或 console.log 因为它们不会向您显示数组结构

您还应该检查是否没有返回数据 (false):

  type: 'POST',
  success:function(data)
    {
        console.dir(data);
        if (data){
            result = JSON.parse(data); 
            $('#test1').append(result[0]['name']);
            $("#test2").append(result[0]['price']);
        }
        else
        {
            $('#test1').append('no records found']);
        } 
    }

【讨论】:

  • 我试过你说的,但有一个错误,我认为它没有读取 id

    严重性:通知

    消息:未定义索引:id

    文件名:models/Booking_model.php

    行号:28

    这里是代码 $id = $_POST["id"]; $query=$this->db->select('name, price') ->where('room_id', $id)
  • 我刚刚注意到:要接收 POST 变量,您需要将 ajax 从 type:'GET' 更改为 type: 'POST'
  • 好先生。非常感谢它现在加载数据!单击button2时如何更改数据?例如我点击button1然后它加载房间的名称和价格,如果我点击button2它将显示button2的数据并删除button1的数据。这是我的代码$("#test1").append(result[0]['name']); $("#test2").append(result[0]['price']);
  • 不客气。请提出一个新问题来解决新问题,请创建一个 Minimal, Complete, and Verifiable example 并阅读有关 What should I do when someone answers my question? 的信息。谢谢。
  • 哦,再次感谢先生!顺便说一句,你能访问我关于我早期评论的问题吗? stackoverflow.com/questions/59127282/jquery-ajax-data-query谢谢先生!
猜你喜欢
  • 1970-01-01
  • 2013-03-16
  • 2019-06-30
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-08
  • 2014-09-18
相关资源
最近更新 更多