Advertisement
Phantom1605

Validasi Form Upload Ajax

Feb 8th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* VALIDASI UNTUK FORM TEXTBOX DAN WORK SEPERTI EKSPETASINYA */
  2.  
  3. function tombol(){
  4.     var NamaBarang = $('#Txt_Nama').val();
  5.  
  6.     if(save_method == 'add'){
  7.         url = site + "barang/simpan";
  8.     }else{
  9.         url = site + "barang/update";
  10.     }
  11.  
  12.     $.ajax({
  13.         url         : url,
  14.         type        : "POST",
  15.         data        : $('#form').serialize(),
  16.         dataType    : "JSON",
  17.         success     : function(data){
  18.             if (data.success == true) {
  19.                 $('.text-message').append('<div class="alert alert-success"><i class="fa fa-check"></i> Data <b>'+ data.result.Txt_Nama +'</b> has been saved</div>');
  20.                 $('.form-group').removeClass('has-error')
  21.                                 .removeClass('has-success');
  22.                 $('.help-block').remove();
  23.  
  24.                 $('.alert').delay(500).show(10, function(){
  25.                     $(this).delay(3000).hide(10, function(){
  26.                         $(this).remove();
  27.                     });
  28.                 });
  29.                
  30.                 tampil_data();
  31.                 pageNormal();
  32.             }else{
  33.                 $.each(data.messages, function(key, value){
  34.                     var element = $('#'+key);
  35.                     element.closest('div.form-group')
  36.                     .removeClass('has-success')
  37.                     .removeClass('has-error')
  38.                     .addClass(value.length > 0 ? 'has-error' : 'has-success')
  39.                     .find('.help-block').remove();
  40.                     element.after(value);
  41.                 });
  42.             }
  43.         },
  44.         error       : function (jqXHR, textStatus, errorThrown){
  45.             alert('Error adding / update data');
  46.         }
  47.     });
  48. }
  49.  
  50.  
  51. /* VALIDASI UNTUK FORM UPLOAD TIDAK WORK */
  52. /* Bagaimana solusinya? Mohon pencerahannya */
  53.  
  54. $(document).ready(function(){
  55.                 $('#upload_file').on('submit', function(e){
  56.                     e.preventDefault();
  57.  
  58.                     /*if($('#Txt_File').val() == ''){
  59.                         alert("Please Select the File");
  60.                     }else{*/
  61.                         $.ajax({
  62.                             url         : site + 'upload/aksi_upload',
  63.                             method      : "POST",
  64.                             data        : new FormData(this),
  65.                             contentType : false,
  66.                             cache       : false,
  67.                             processData : false,
  68.                             success     : function(data){
  69.                                 if (data.status == true) {
  70.                                     $('.text-message').append('<div class="alert alert-success"><i class="fa fa-check"></i> Data <b>'+ data.result.file_name +'</b> has been saved</div>');
  71.                                     $('.form-group').removeClass('has-error')
  72.                                                     .removeClass('has-success');
  73.  
  74.                                     $('.alert').delay(500).show(10, function(){
  75.                                         $(this).delay(3000).hide(10, function(){
  76.                                             $(this).remove();
  77.                                         });
  78.                                     });
  79.                                 }else{
  80.                                     $.each(data.messages, function(key, value){
  81.                                         /*console.log(key);*/
  82.                                         var element = $('#'+key);
  83.  
  84.                                         element.closest('div.form-group')
  85.                                         .removeClass('has-success')
  86.                                         .removeClass('has-error')
  87.                                         .addClass(value.length > 0 ? 'has-error' : 'has-success')
  88.                                         .find('.help-block').remove();
  89.  
  90.                                         element.after(value);
  91.                                     });
  92.                                 }
  93.                             }
  94.                         });
  95.                     // }
  96.                 });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement