1

how to pass the database fetched values from controller to view page using ajax and json.i have a controller and model code but i dont know how to pass values from controller to view page

Controller

public function supplier_get_data(){


        $query = $this->Profile_model->supplier_communication();

        echo json_encode($query);

    }

model

 public function supplier_communication(){
        $sql = $this->db->get('communication');
        $sql = $sql->result_array();
        return $sql;
    }

view page

<div class="left">

                                            <div class="row">
                                                <div class="col-md-1">
                                                    <img src="<?php echo base_url(); ?>images/xxxx.jpg" class="img-circle" width="30px" height="30px"/>
                                                </div>
                                                <div class="col-md-11">

                                                    <div class="left_msg_block">



                                                        <div class="left_messagetext"><a href="#" data-toggle="tooltip" title="time">here i want to display the database values</a></div>
                                                  </div>
                                                </div>
                                            </div>

                                        </div>
3
  • How did you want your view page result? Commented Jun 3, 2017 at 14:11
  • Is that the pattern you need <a href="#" data-toggle="tooltip" title="time">value1</a> <a href="#" data-toggle="tooltip" title="time">value2</a> Commented Jun 3, 2017 at 14:13
  • @ubm yes same patten i want Commented Jun 5, 2017 at 5:08

1 Answer 1

1

This code may helps you Controller function

public function supplier_get_data(){


    $query = $this->Profile_model->supplier_communication();

    echo json_encode($query);

}

Model function

public function supplier_communication(){
    $sql = $this->db->get('communication');
    $sql = $sql->result();
    return $sql;
}

Ajax function

 function loadData()
    {
    $.ajax({
    type:"post",
    url:"<?php echo site_url('controllername/supplier_get_data')?>",
    dataType:"json",
    success:function(data){
      for(i=0;i<data.length;i++){
     alert(data[i].columnanme)
      $('<a href="#" data-toggle="tooltip" title="time">'+data[i].columnanme+'</a>').appendTo('.left_messagetext');
      }
    }

  });
 }
Sign up to request clarification or add additional context in comments.

1 Comment

in view page i want to fetch the values in this line <div class="left_messagetext"><a href="#" data-toggle="tooltip" title="time">here i want to display the database values</a></div>

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.