1

I am new in codeigniter.

I want to display data in codeigniter view div id from ajax on window load.

View.php

<script type='text/javascript' language='javascript'>
$(window).load(function() 
{
    $.ajax({
       type: "POST",
       url: "<?php echo base_url(); ?>colleges/index",       
       success: function(server_response
       {
           if(server_response == 'success')
           {
              $("#fillgrid").html(server_response); 
           } 
           else{
              alert('Not OKay');
           }
                      }
   });   //$.ajax ends here
});
</script>
1
  • 1
    What is the problem here? Are you stuck somewhere? Commented Jul 15, 2016 at 17:42

1 Answer 1

0

I added explanations directly in code:

$(window).load(function() 
{
    $.ajax({
       type: "POST",
       url: "<?php echo base_url(); ?>colleges/index",       
       success: function(server_response)           // Added missing parenthesis
       {
           if(server_response == 'success')         // You already are in the success callback.
           {                                        
              $("#fillgrid").html(server_response); 
           } 
           else{
              alert('Not OKay');
           }
                      }
   });   //$.ajax ends here
});

Function simplifed:

$.ajax({
    type: "POST",
    url: "<?php echo base_url(); ?>colleges/index",       
    success: function(server_response){
        $("#fillgrid").html(server_response); 
    },
    error: function(){
        alert('Not Okay');
    }
});   //$.ajax ends here

Don't place an Ajax function inside a .load() method !
.load is a jQuery shorthand for .ajax().
So it's one or the other... But not both.

Using .load(), it would be:

$("#fillgrid").load("<?php echo base_url(); ?>colleges/index");
Sign up to request clarification or add additional context in comments.

5 Comments

In my model.php public function get_all_college() { $this->db->order_by("id","DESC"); $query = $this->db->get("tbl_colleges"); return $query->result_array(); } In my controller.php public function index() { $list['college'] = $this->Colleges_model->get_all_college(); $this->load->view('header'); $this->load->view('Colleges/all_colleges', $list); $this->load->view('footer'); } It is right above code.
Ajax server_response contains the html produced. I'm not really used to model/controller/view framework... You have to make something to echo the result.
«It is right above code.» It does'nt matter where the PHP script is versus the jQuery script. PHP executes server-side... And jQuery executes on client-side, "long" after the PHP. ;) So here, you produce a page that recalls PHP again... I hope you don't mke the common error : a page that calls itself... The ajax called content should be another PHP script than the one of the main page.
Here is a previous answer I made to "explain" things about a working Ajax example... May be usfefull to you. stackoverflow.com/questions/38187894/…
Hey!! I just saw that!!! You placed an Ajax call inside a .load() method ?!?! I edited my answer.

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.