i'm working on simple insert , update data form ajax , codeigniter. been struggling because i'm new in ajax. insert method work, when want data id
, update same modal, won't load.
here code:
user controller :
/* location: ./application/controllers/master/user.php */ function insert() { $id_user = addslashes($this->input->post('id_user')); $username = addslashes($this->input->post('username')); $password = addslashes($this->input->post('password')); $email = addslashes($this->input->post('email')); $data = array( 'username' => $username, 'password' => $password, 'email' => $email ); $this->m_user->set_insert($data); redirect('master/user'); } function update() { $id_user = addslashes($this->input->post('id_user')); $username = addslashes($this->input->post('username')); $password = addslashes($this->input->post('password')); $email = addslashes($this->input->post('email')); $data = array( 'username' => $username, 'password' => $password, 'email' => $email ); $this->m_user->set_update($id_user,$data); redirect('master/user'); }
user model :
function set_insert($data){ $this->db->insert($this->table, $data); return true; } function set_update($id_user,$data) { $this->db->where('id_user', $id_user); $this->db->update($this->table, $data); return true; }
and here view:
<!-- main content --> <section class="content"> <!-- /.box-header --> <div class="box-body"> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#btnmodalform">add user</button><br><br> [.....] <!-- modal data --> <div class="row"> <div class="col-lg-12"> <!-- modal --> <div class="modal draggable fade" id="btnmodalform" tabindex="-1" role="dialog" aria-labelledby="btnmodalformlabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="btnmodalformlabel"> data user</h4> </div> <form id="addform" method="post" action="<?php echo site_url('master/user/insert') ?>" class="form-horizontal"> <table class="display table table-striped table-bordered table-hover" cellspacing="0" width="100%"> <tr> <td class="col-sm-2 control-label">username</td> <td> <div class="col-sm-4"> <input type="text" name="username" class="form-control" value=""> </div> </td> </tr> <tr> <td class="col-sm-2 control-label">password</td> <td> <div class="col-sm-4"> <input type="text" name="password" class="form-control" value=""> </div> </td> </tr> <tr> <td class="col-sm-2 control-label">email</td> <td> <div class="col-sm-6"> <input type="text" name="email" class="form-control" value=""> </div> </td> </tr> <tr> <td colspan="2"> <button type="button" class="btn btn-default" data-dismiss="modal">close</button> <button type="submit" id="btnsavedata" class="btn btn-primary">save</button> </td> </tr> </table> </form> </div> </div> </div> </div> </div> <!-- modal data --> </div> <!-- /.box-body --> </section> <script type="text/javascript"> $(document).ready(function() { $('#btnmodalform').on('shown.bs.modal') $('#btnmodalform, #btnupdate').draggable({ cursor: 'move', handle: '.modal-header' }); $('#btnsavedata').bind('click', function(event) { var v_username = $('input:text[name-username]').val(); var v_password = $('input:text[name-password]').val(); var v_email = $('input:text[name-email]').val(); $.ajax({ type: "post", data: {username: v_username, password:v_password, email:v_email}, datatype: 'json', success: function() { location.reload(true); }, error: function(xhr, textstatus, errorthrown){ location.reload(); } }) }) $('#btnupdate').on('shown.bs.modal', function () { var v_username = $('input:text[name-username]').val(); var v_password = $('input:text[name-password]').val(); var v_email = $('input:text[name-email]').val(); $.ajax({ url: "<?php echo site_url('master/user/detail') ?>", type: "get", data: {username: v_username, password:v_password, email:v_email}, datatype: 'json', success: function(data) { console.log(data); }, error: function(xhr, textstatus, errorthrown){ // location.reload(); } }) }); }) </script>
maybe made wrong code, appreciated. thanks.
Comments
Post a Comment