2

I'm new in ajax and I don't know how to get over this error. I don't understand this error. Please help.

Here is my Modal User_m.

I fetch data from database successfully

    <?php
  class Users_m extends CI_Model
  {
    public function showAllAdmins()
    {
      $query  = $this->db->get('admin_users');
      if($query->num_rows() > 0){
          return $query->result();
      }else{
          return false;
      }
    }
  }
?>

Here is my Controller AdminUsers

I get all data from database through my modal and encode it using by json_encode but I don't know why this json data is showing on my page header.In this Image

  class AdminUsers extends MY_Controller
  {
    public function __construct()
    {
      parent::__construct();
      $this->load->model('AdminUsers/Users_m');
    }

    public function index()
    {
      $data['content_view'] = 'AdminUsers/viw_users';
      $data['page_title']   = 'Users';
      $data['page_descr']   = 'all admin users';
      $result  = $this->Users_m->showAllAdmins();

      echo json_encode($result);
      $this->template->admin_template($data);
    }
}

And this is my ajax code

When I Every time refresh my page it execute alert Could not get data from database. I don't know How to pass Json data in ajax. I see console log but not show any error. I load jQuery also. Any help will be appreciated. Thanks for your answers. And when I loop through this it goes to infinity.

 $(function(){
      showAllAdmins();

        function showAllAdmins(){
          $.ajax({
              type:'ajax',
              url: 'http://localhost/hmvcExample/AdminUsers',
              async:false,
              success: function(data){
                var html  = '';
                var i = 0;
                for(i=0; i<data.length; i++){
                    html +='<tr>'+
                              '<td>Rakesh</td>'+
                              '<td>Kumar</td>'+
                              '<td>[email protected]</td>'+
                              '<td>Rocky</td>'+
                              '<td><a href="#"><button class="btn btn-primary">Edit</button></a> <a href="#"><button class="btn btn-danger">Delete</button></a></td>'+
                            '</tr>';
                }
                $('#showData').html(html);
              },
              error: function(){
                alert('Could not load Data from Database');
              }

          });
        }
    });

View file

<div class="box">
  <div class="box-header">
    <h3 class="box-title">Users List</h3>
    <button class="btn btn-success pull-right">Add New</button>
  </div>
  <!-- /.box-header -->
  <div class="box-body">
    <table id="example2" class="table table-bordered table-hover table-responsive">
      <thead>
      <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>Email Address</th>
        <th>User Name</th>
        <th>Action</th>
      </tr>
      </thead>
      <tbody id="showData">

      </tbody>
    </table>
  </div>
  <!-- /.box-body -->
</div>
9
  • Did you look into network tab under developer tools? Please check what error you are getting. Commented Nov 26, 2017 at 14:04
  • @prabhu first of all thanks. It showing me Request method is GET and Status Code is 200 OK Commented Nov 26, 2017 at 14:09
  • By looking at your image I can see bootstrap datatable. Have you initialized the datatable? And I didn't understand why you using ajax for rendering?? Directly you can pass the data to the view. Make things simple Commented Nov 26, 2017 at 14:11
  • @prabhu Initialized datatable? Okay but I don't know how to do it. and Please can you tell me why json data is showing on header part. Commented Nov 26, 2017 at 14:17
  • Please refer documentation datatables.net/examples/styling/bootstrap.html By looking at the image the datatable is already seem to be initialized. Since you are using ajax dynamically data is assigned. So it has to be initialized again. Commented Nov 26, 2017 at 14:21

1 Answer 1

1

I'm posting a sample code. Please refer.

<table id="example1" class="table table-bordered table-striped">
                    <thead>
                    <tr>
                        <th>Slno</th>
                        <th>Name</th>
                        <th>Username</th>
                        <th>Email</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                    </thead>
                    <tbody id="user_table">
                        <?php if($result) { $i =1; foreach ($result as $row){ ?>
                        <tr>
                            <td><?php echo $i; ?></td>
                            <td><?php echo $row->fullname; ?></td>
                            <td><?php echo $row->username; ?></td>
                            <td><?php echo $row->email; ?></td>
                            <td><?php if($row->active == "1"){ ?>
                                <span class="label label-success"><?php echo 'Active'; ?></span>  
                            <?php }elseif($row->active == "0"){ ?>
                                <span class="label label-warning"><?php echo 'Inactive'; ?></span>
                            <?php } ?></td>
                            <td>
                                <div class="btn-group">
                                    <a class="btn btn-xs btn-success" href="<?php echo base_url().'admin/user/view_user?id='.$row->user_id.'&action=edit'; ?>" title="View">
                                      <i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <?php  $i++; } } ?>
                    </tbody>
                  </table>

You have to pass the data from the controller as follows

$data['result'] = $result; //your sql query resultset
$this->template->admin_template($data);

No need to use ajax at all. Hope this can help you. Feel free to ask if you have any queries.

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

6 Comments

Thanks for your code. I work on CRUD and I want to do this using ajax. I add user, edit , delete and read on same page without reloading page. currently I just show data from database using ajax and after it I move on other things. Hope you understand my questions. Thanks!
Dude for the first time, when the page is rendering, and if you make an ajax call you are delaying the page load. Make some research before you code. Your method is improper. After the page rendering you can make any number ajax calls. Understand the purpose of it.
Thanks for your time. I follow your instructions. Anyway thanks for you answer
If you feel my answer has helped you somewhere, please accept and upvote my answer, so that others can get help out of it.
But I have not enough Reputation to up vote your answer.. Sorry for that
|

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.