php - Codeigniter ajax get data from mysql and display in html form -
i have list of names display , when user clicks on edit, opens dialog html form. form show values of data retrieved mysql table. confused how can achieve that. have far.
view
<table class="table"> <thead> <tr> <th>no.</th> <th>organisation</th> <th>first name</th> <th>last name</th> <th>privilege level</th> <th>date added</th> <th></th> <th></th> </tr> </thead> <tbody> <?php $offset = $this->uri->segment(4,0)+1; ?> <?php foreach($user $row): ?> <tr data-id="<?php echo $row->id; ?>"> <td><?php echo $offset++; ?></td> <td><?php echo $row->company; ?></td> <td><?php echo $row->firstname; ?></td> <td><?php echo $row->lastname; ?></td> <td><?php echo $row->privilege; ?></td> <td><?php echo $row->date_added; ?></td> <td><input type="hidden" name="id" value="<?php echo $row->id; ?>"/><input type="hidden" name="company" value="<?php echo $row->id; ?>"/><input type="hidden" name="set" value="edit"/><input type="button" class="editlink" value="edit"/></td> <td><input type="hidden" name="id" value="<?php echo $row->id; ?>"/><input type="hidden" name="company" value="<?php echo $row->id; ?>"/><input type="hidden" name="set" value="delete"/><input type="button" class="deletelink" value="delete"/></td> </tr> <?php endforeach; ?> </tbody> </table> <?php echo $links; ?> </div> <div id="edit-dialog"> <?php echo form_open('manager/users/edit_user'); ?> <p>first name : <input type="text" name="firstname"/></p> <p>last name : <input type="text" name="lastname"/></p> <p>nric : <input type="text" name="nric"/></p> <p>address : <input type="text" name="address"/></p> <p>username : <input type="text" name="username"/></p> </form> </div>
my controller
function get_user() { $id = $this->input->post('id'); $this->load->model('user'); $data = $this->user->retrieve($id); }
my model
public function generate($id) { $this->db->where('id',$id); $query = $this->db->get($this->tablename); if($query->num_rows()==1) { return $query->result(); } else { return $query->result(); } }
my jquery/ajax
$('.editlink').on('click', function() { var user_id = $(this).parent().parent('tr').attr('data-id'); $.ajax({ url: 'http://localhost/manager/users/get_user', type: 'post', datatype:'text', data:{'id':user_id}, cache:false, success:function(result){ $("#edit-dialog").show(); console.log($.parsejson(result)); $("#edit-dialog").dialog({ title: 'edit user', width: 400, height: 500, draggable: false, modal: true, dialogclass: "edit-user", open: function(){ }, buttons: [ { text: 'update', click: function() { $('#manipulate_user').submit(); } }, { text: 'cancel', click: function() { $(this).dialog('close'); } } ] }); }}); }); });
Comments
Post a Comment