php - codeigniter send id and get database values without refresh

500

I need to send id and get database values according to the id without page refresh.and display data in my view,

here is my view,

 <div>
 <a href="javascript:void(0);" class="movie" onclick="getSummary(24)">Click on me</a>
 </div>
<script type="text/javascript">
    function getSummary(id)
    {
       $.ajax({

         type: "POST",
         url: '<?php echo site_url('ajax_controller/getBranchDetails'); ?>',
         cache:false,
         data: "id=" + id, // appears as $_GET['id'] @ ur backend side
         success: function(data) {
               // data is ur summary
              $('#summary').html(data);
         }

       });

    }
</script>

controller

public function getBranchDetails(){

$b_id = $this->input->post('branch_id');
$this->load->model('ajax_model');
$data['results'] = $this->ajax_model->getRecords($b_id);


 //echo json_encode(array('data'=>$data));
}

I need to display $data['results'] in my view

model

    <?php

    class Ajax_model extends CI_model{

    function getRecords($id)
    {
        $this->load->database();
        $query = $this->db->query("SELECT * FROM companydetails WHERE id='$id'");
        return $query->result();

    }

}

365

Answer

Solution:

Try this code ,It is working .

    <script type="text/javascript">
    function getSummary(id)
    {
     $.post("<?php echo site_url('ajax_controller/getBranchDetails'); ?>",
        {branch_id:id},function(data,status){
              $('#summary').html(data);
    });
    }
    </script>

Note : Check whether you have included jquery.min.js . If not mention the below code in view part header

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">

791

Answer

Solution:

Try this instead:

<script type="text/javascript">
function getSummary(id) {
  $.ajax({
    type: 'POST',
    url: "<?php echo site_url('ajax_controller/getBranchDetails'); ?>", // <-- properly quote this line
    cache: false,
    data: {branch_id: id}, // <-- provide the branch_id so it will be used as $_POST['branch_id']
    dataType: 'JSON', // <-- add json datatype
    success: function(data) {
        // if you need to present this in a html table,
        // likely, you need to use $.each() and build the markup using the json response 
        $('#summary').html(data);
    }
  });
}
</script>

Model: (Don't directly use variables inside the query string)

$sql = 'SELECT * FROM companydetails WHERE id = ?';
$query = $this->db->query($sql, array($id));

Controller:

public function getBranchDetails()
{
    $b_id = $this->input->post('branch_id', true);
    $this->load->model('ajax_model');
    $data['results'] = $this->ajax_model->getRecords($b_id);
    echo json_encode($data); // <-- uncomment
}

People are also looking for solutions to the problem: php - Stop jqueryUI Tab 0 from loading by default

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.