how can i get the data from my database when i click each of my button's id my controller function
public function ajax (){
$resultdata = $this->Booking_model->roominfo();
echo json_encode($resultdata);
}
here's my button with its data-room-id and class
<button class="bkng bkn-room trans_200" data-room-id ="1">BOOK NOW</button>
here's my model
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();
//}
my ajax jquery with its button class and data-room-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);
}
});
}
});
});
it always says in the console error that Uncaught TypeError: Cannot read property 'name' of undefined
console.log(result)contains name attribute?nameattribute ?