2

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

3
  • console.log(result) contains name attribute? Commented Nov 29, 2019 at 21:59
  • @Jed does your query returns the name attribute ? Commented Nov 30, 2019 at 7:17
  • no it does not return the name attribute it cannot read the [0] and the name property i dont know what to do Commented Nov 30, 2019 at 14:20

1 Answer 1

1

you get this error, because your query is not returning anything, as you might not have a room with the id = 'id'. Therefore JSON.parse returns this error.

so first you need to fix your model building a valid query, like:

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;  
}   

then in your ajax success, you can check for the data returned with: console.dir(data) and not with alert or console.log as they will not show you the array structure

you also should check the eventuality of no data (false) was returned:

  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']);
        } 
    }
Sign up to request clarification or add additional context in comments.

5 Comments

i tried what you said but there's an error i think it doesnt read the id <p>Severity: Notice</p> <p>Message: Undefined index: id</p> <p>Filename: models/Booking_model.php</p> <p>Line Number: 28</p> here's the code $id = $_POST["id"]; $query=$this->db->select('name, price') ->where('room_id', $id)
I just noticed: to receive the POST variable you need to change your ajax from type:'GET' to type: 'POST'
nice sir. thank you very much it now loads the data! How change i change the data when i click the button2? for example i click button1 then it loads the name and the price of the room, if i click button2 it will show the data of the button2 and remove the data of the button1. this is my code $("#test1").append(result[0]['name']); $("#test2").append(result[0]['price']);
You are welcome. Please ask a new question to cover the new problem, please create a create a Minimal, Complete, and Verifiable example and also read about What should I do when someone answers my question?. Thanks.
oh thanks again sir! btw can you please visit my question about my early comment? stackoverflow.com/questions/59127282/jquery-ajax-data-query thanks sir!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.