CodeIgniter Forums

Full Version: Ajax output problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I'm newbie in ajax, my problem is my ajax just can open 1st row only and 2nd row i receive null no data retrieve from ajax

this is my AJAX problem.

 
Code:
<!-- ajax req -->
    <script type="application/javascript">
            $('.dropdown-menu').on('click', '.payment_proof-show ', function(){
                var user_id = $(this).attr('data');
                //$('#modal_paymentProof').attr('id', "modal_paymentProof"+user_id);
                $.ajax({
                    type: 'ajax',
                    method: 'get',
                    url: '<?php echo base_url() ?>download/payment_proof_detail',
                    data: {user_id: user_id},
                    async: false,
                    dataType: 'json',
                    success: function(data) {
                        var html= '';
                        var i;
                        for(i=1; i<data.length; i++){
                            html+='<tr>'+
                                '<td>'+i+ user_id +'</td>'+
                                '<td>'+data[i].remarks+'</td>'+
                                '<td>'+data[i].user_id+'</td>'+
                                '<td><a href="<?php echo base_url() ?>Admin/payment_proof_download/'+data[i].payment_proof+'" class="btn btn-info"> <i class="fa fa-download fa-size-medium"></i> Download</a></td>'+
                             '</tr>';

                        }

                        $('#payment-proof').html(html);
                    },
                    error: function() {
                        alert('Something going wrong');
                    }
                });
            });
    </script>

this is my view

PHP Code:
<body>
 
 <!--<div class="container"> -->
 
   <div class="col-md-12">
 
     <div class="well">

 
       <fieldset>
 
         <h4>User List</h4>
 
         <hr>
 
         <div class="form-group">
 
             <?php
              echo form_open
('Admin/search_user');
 
               ?>
              <div class="col-md-5 input-group">
                <span class="input-group-addon"><i class="fa  fa-search fa-size-medium"></i></span>
                <input type="text" name="search_user" class="form-control">
                <span class="input-group-btn">
                  <button class="btn btn-default" type="submit">Search</button>
                </span>

              </div>
              <p class="help-block">User ID, Full Name, State, Phone Number, and IC No.</p>
            </form>

          </div>
          <div class="table-responsive">
            <table class="table table-striped table-hover">
              <th>Username/ID</th>
              <th>Active Date</th>
              <th>Full Name</th>
              <th>Mobile No.</th>

              <th>State</th>
              <th>Member Type</th>
              <th>Set(S)</th>
              <th>Correspondent Name</th>
              <th>Create User</th>
              <th>Create Date</th>
              <th colspan="2">Action</th>
              <?php

              foreach 
($get_list as $list) {
 
               echo '
                <tr>
                    <td>'
.$list['username'].'</td>
                    <td>'
.date'd/m/Y'strtotime($list['active_date'])).'</td>
                    <td>'
.$list['name'].'</td>
                    <td>'
.$list['mobile_number'].'</td>
                    <td>'
.$list['state'].'</td>
                    <td>'
;
 
                     if ($list['auth_level'] == 1)
 
                     {
 
                         echo '<span class="label label-default">Member</span>';
 
                     }
 
                     elseif ($list['auth_level'] == 2)
 
                     {
 
                         echo '<span class="label label-primary">Stokis</span>';
 
                     }
 
                     elseif ($list['auth_level'] == 3)
 
                     {
 
                       echo '<span class="label label-primary">Vendor</span>';
 
                     }

 
                   echo '</td>
                    <td>'
.$list['total_set'].'</td>
                    <td>'
.$list['name_crrespondent'].'</td>
                    <td>'
.$list['correspondent_id_no'].'</td>
                    <td>'
.date'd/m/Y'strtotime($list['register_date'])).'</td>
                    <td>
                    <div class="btn-group pull-right">
                      <a href="#" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                        Select Action
                        <span class="caret"></span>
                      </a>
                      <ul class="dropdown-menu">
                        <li><a href="'
.base_url('Admin/basic_info/'.$list['user_id']).'"><i class="fa fa-user-circle-o"></i> Basic Info</a></li>
                        <li><a href="'
.base_url('Admin/userview/'.$list['user_id']).'"><i class="fa fa-pencil"></i> View / Edit</a></li>
                        <li><a href="'
.base_url('Admin_Auth/login_detail/'.$list['user_id']).'"><i class="fa fa-vcard"></i> Login Detail</a></li>
                        <li><a class="payment_proof-show" data-target="#modal_paymentProof'
.$list['user_id'].'"  data-toggle="modal" data="'.$list['user_id'].'"><i class="fa  fa-usd"></i> Payment Proof</a></li>
                        <li><a href="'
.base_url('Admin_Auth/resetUserPassword/'.$list['user_id']).'"><i class="fa  fa-unlock-alt"></i> Reset Password</a></li>
                        <li><a href="#"><i class="fa fa-remove"></i> Delete</a></li>
                       </ul>
                    </div>
                    </td>
                    <td>
                        <a href="'
.base_url('Admin/add_set/'.$list['user_id']).'" class="btn btn-info">
                        <i class="fa fa-plus fa-size-medium"></i> Add Set</a>
                   </td>
                  </tr>'
;
 
             }


 
             ?>
            </table>
          </div><!-- table responsive -->
        </fieldset>
      </div>
    </div>
    <?php

      foreach 
($get_list as $list) {
 
       echo '<div  class="modal fade" id="modal_paymentProof'.$list['user_id'].'">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Payment Prooft</h4>
              </div>
              <div class="modal-body">
              <p>
              <div class="form-horizontal">
              <div class="form-group">
                <label for="username" class="control-label col-sm-2">Username</label>
                  <div class="col-sm-10">
                    <input type="text" class="form-control uppercase" id="username" name="username" value="'
.$list['username'].'" disabled="">
                  </div>
                </div>
                <div class="form-group">
                  <label for="name" class="control-label col-sm-2">Name</label>
                  <div class="col-sm-10">
                    <input type="text" class="form-control uppercase" id="name" name="name" value="'
.$list['name'].'" disabled="">
                  </div>
                  </div>
              </div>
              <h5>File List</h5>
              <hr>
              <table class="table">
              <thead>
                  <th>No.</th>
                  <th>Remarks</th>
                  <th>Action</th>
                </thead>
                <tbody id="payment-proof">

                </tbody>
              </table>
              </p>
              </div><!-- /modal-body -->
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              </div>
            </div>
          </div>
        </div>'
;
 
     }

 
   ?>

  <!-- </div> end container --> 

this is my controller
PHP Code:
public function payment_proof_detail()
    {
        
$user_id $this->input->get('user_id');
        
$result $this->Auth_Model->get_payment_proof($user_id);
        echo 
json_encode($result);

    } 

this is my modal
PHP Code:
function get_payment_proof($id)
 
 {
 
   $data = array();
 
   $this->db->select('*');
 
   $this->db->from('payment_proof');
 
   $this->db->where('user_id'$id);
 
   $query $this->db->get();

 
   if ($query->num_rows() > 0)
 
   {
 
       foreach ($query->result_array() as $row)
 
       {
 
           $data[] = $row;
 
       }
 
   }
 
   $query->free_result();
 
   return $data;

 
 

Help me. i can't sleep bcoz this.
You forgot the semi-colon after echo base_url(). Because of that, php will generate an error.
Besides that, I recommend letting the controller return the complete html output (you can use a view to do that) instead of building the dynamic html with Javascript.
(03-26-2017, 05:33 AM)Wouter60 Wrote: [ -> ]You forgot the semi-colon after echo base_url(). Because of that, php will generate an error.
Besides that, I recommend letting the controller return the complete html output (you can use a view to do that) instead of building the dynamic html with Javascript.

its not working.  Sad my 1st row data ajax can retrieve data. After i click 2nd row its not show anything. btw thanks coz reply.
I suggest you to make it work without ajax, and then implement ajax version. 


1. 
PHP Code:
       foreach ($query->result_array() as $row)
 
       {
 
           $data[] = $row;
 
       
I did not understand why dont u use $data = $query->result_array(); this way.

2.
in your javascript code start your index i from 0;