Advertisement
amran_87

Update function - URL name

Apr 14th, 2020
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.79 KB | None | 0 0
  1. $(document).ready(function() {
  2.     let financing_form = $('#financing_form');
  3.     let alert_message = $('#financing_form .alert');
  4.  
  5.     // dapatkan alamat current url
  6.     let url = window.location.href;
  7.     // tukar pada bentuk array
  8.     // cth: http://ppsis:8080/restructures/1/edit
  9.     // [http, ,ppsis:8080, restructures, ...]
  10.     let url_array = url.split('/');
  11.     let input_types = $('.types');
  12.     console.log(url_array[3]);
  13.  
  14.     // input value mengikut menu yg dipilih. Sekiranya ingin buat nama lain,
  15.     // boleh gunakan if..else utk define value tersebut.
  16.     // if (url_array[3] === 'financings') {
  17.         // input_types.val('FIN');
  18.         input_types.val(url_array[3]);
  19.     // }
  20.  
  21.     $('#financing_form .alert').hide();
  22.  
  23.     financing_form.submit(function(e) {
  24.         e.preventDefault();
  25.         let form_url = $(this).attr('action');
  26.         let form_method = $(this).attr('method');
  27.         alert_message.removeClass('alert-danger alert-success');
  28.         alert_message.show();
  29.  
  30.         $.ajax({
  31.             // url: '/financings',
  32.             // type: 'POST',
  33.             url: form_url,
  34.             type: form_method,
  35.             dataType: 'json',
  36.             data: financing_form.serialize()
  37.         })
  38.         .done(function(data) {
  39.             alert_message.addClass('alert-success');
  40.             $('.alert.alert-success').html('Success add data');
  41.             $('.modal').animate({scrollTop: '11px'}, 500);
  42.             console.log(data);
  43.         })
  44.         .fail(function(data) {
  45.             alert_message.addClass('alert-danger');
  46.             let errors = data.responseJSON.errors;
  47.             let error_msg = "";
  48.             console.log("error: ");
  49.             console.log(errors);
  50.            
  51.             for(let error in errors){
  52.                 console.log(errors[error]);
  53.                 error_msg += '<li>' + errors[error] + '</li>';
  54.             }
  55.  
  56.             $('.alert.alert-danger').html("<ul>" + error_msg + "</ul>");           
  57.             $('.modal').animate({scrollTop: '11px'}, 500);
  58.         })
  59.         .always(function() {
  60.             console.log("complete");
  61.         });
  62.     });
  63. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement