Guest User

CRUD Modal Bootstrap script.js

a guest
May 26th, 2018
8,757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. $(document).ready(function(){
  3.     var data = "data.php";
  4.     $('#data-mahasiswa').load(data);
  5.    
  6.     $("#form-tambah").submit(function(e) {
  7.         e.preventDefault();
  8.        
  9.         $("#error_nama").html('');
  10.         $("#error_jenis_kelamin").html('');
  11.         $("#error_telepon").html('');
  12.         $("#error_alamat").html('');
  13.        
  14.         var dataform = $("#form-tambah").serialize();
  15.         $.ajax({
  16.             url: "input.php",
  17.             type: "post",
  18.             data: dataform,
  19.             success: function(result) {
  20.                 var hasil = JSON.parse(result);
  21.                 if (hasil.hasil !== "sukses") {
  22.                     $("#error_nama").html(hasil.error.nama);
  23.                     $("#error_jenis_kelamin").html(hasil.error.jenis_kelamin);
  24.                     $("#error_telepon").html(hasil.error.telepon);
  25.                     $("#error_alamat").html(hasil.error.alamat);
  26.                 } else {
  27.                     $('#modal-tambah').modal('hide');
  28.                     $("#nama").val('');
  29.                     $("#jenis_kelamin").val('');
  30.                     $("#telepon").val('');
  31.                     $("#alamat").val('');
  32.                     $('#data-mahasiswa').load(data);
  33.                 }
  34.             }
  35.         });
  36.     });
  37.    
  38.     $(document).on('click','#edit',function(e){
  39.         e.preventDefault();
  40.         $("#modal-edit").modal('show');
  41.         $.post('edit.php',
  42.             {id:$(this).attr('data-id')},
  43.             function(html){
  44.                 $("#data-edit").html(html);
  45.             }  
  46.         );
  47.     });
  48.    
  49.    
  50.     $("#form-edit").submit(function(e) {
  51.         e.preventDefault();
  52.        
  53.         $("#error_nama").html('');
  54.         $("#error_jenis_kelamin").html('');
  55.         $("#error_telepon").html('');
  56.         $("#error_alamat").html('');
  57.        
  58.         var dataform = $("#form-edit").serialize();
  59.         $.ajax({
  60.             url: "update.php",
  61.             type: "post",
  62.             data: dataform,
  63.             success: function(result) {
  64.                 var hasil = JSON.parse(result);
  65.                 if (hasil.hasil !== "sukses") {
  66.                     $("#error_edit_nama").html(hasil.error.edit_nama);
  67.                     $("#error_edit_jenis_kelamin").html(hasil.error.edit_jenis_kelamin);
  68.                     $("#error_edit_telepon").html(hasil.error.edit_telepon);
  69.                     $("#error_edit_alamat").html(hasil.error.edit_alamat);
  70.                 } else {
  71.                     $('#modal-edit').modal('hide');
  72.                     $("#nama").val('');
  73.                     $("#jenis_kelamin").val('');
  74.                     $("#telepon").val('');
  75.                     $("#alamat").val('');
  76.                     $('#data-mahasiswa').load(data);
  77.                 }
  78.             }
  79.         });
  80.     });
  81.    
  82.    
  83.     $(document).on('click','#hapus',function(e){
  84.         e.preventDefault();
  85.         $.post('hapus.php',
  86.             {id:$(this).attr('data-id')},
  87.             function(html){
  88.                 $('#data-mahasiswa').load(data);           
  89.             }  
  90.         );
  91.     });
  92.  
  93.  
  94. });
Advertisement
Add Comment
Please, Sign In to add comment