Click here to Skip to main content
14,934,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to refresh / reload the page using ajax with codeigniter.
The code is working fine but it is loading my whole page [view] and i want to only load the table or some specific container but using the same view. I check a lot of question but did not find what i want, Please check the question instead of down voting. What is wrong in it? can someone please look at it.

What I have tried:

View:

      <pre lang="PHP"><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Insert | SP</title>
      <script src="<?php echo base_url(); ?>bootstrap/js/jquery-2.1.1.min.js"></script>

<script>
        $(document).ready(function(){

            function loadNowPlaying(){
      $(this).load('<?php echo base_url().'Store/student' ;?>');
    }
    setInterval(function(){loadNowPlaying()}, 5000);
        });


        </script>

 </head>
    <body>
        <form action="<?php echo base_url();?>Store/add" method="post">
            <input type="text" name="name" id="name"><br>
            <input type="text" name="mail" id="mail"><br>
            <input type="password" name="pass" id="pass"><br>
            <input type="submit" name="submit" id="submit" value="Insert">
        </form>


        <table id="studata">
           <?php foreach ($students as $student):?>
            <tr>
               <th>Name</th>
               <th>Email</th>
               <th>Password</th>
            </tr>
               <tr>
                <td><?php echo $student->Name;?></td>
                <td><?php echo $student->Email;?></td>
                <td><?php echo $student->Password;?></td>
            </tr>
            <?php endforeach;?>
        </table>
    </body>
    </html>


My Controller code:



PHP
public function student()
    {
        $data["students"] = $this->sp->getstudent();
        $this->load->view('student',$data);
    }


My Model:


PHP
public function getstudent()
    {
         $this->db->select('*');
        $this->db->from('ex_sp');
        $query = $this->db->get();
        return $query->result();
    }
Posted
Updated 25-Aug-17 5:34am

1 solution

Quote:
$(this).load('<?php echo base_url().'Store/student' ;?>');

Load data from the server and place the returned HTML into the matched element.

What do you imagine the "matched element" is in your code? (NB: You can use the debugger to find out!)

If you want to replace a specific element, then you need to match that element before calling load. For example, to replace the studata table with the content of the loaded document, use:
JavaScript
$("#studata").load('<?php echo base_url().'Store/student' ;?>');

If the page you're loading returns an entire HTML document, and you only want to load a specific part of it, pass a selector to the URL. For example, to replace the studata table on the current page with the studata table on the loaded page, use:
JavaScript
$("#studata").load('<?php echo base_url().'Store/student' ;?> #studata');
   
Comments
msz900 25-Aug-17 13:15pm
   
still not working, it loads full pages instead of specific area.
Richard Deeming 25-Aug-17 13:17pm
   
Then you probably have an error in your code. Use the browser developer tools to debug your script.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900