【发布时间】: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