Home Is this code to submit multiple forms good or should I use a different approach?
Reply: 0

Is this code to submit multiple forms good or should I use a different approach?

user1602
1#
user1602 Published in April 22, 2018, 10:06 pm

I hava a one page website with multiple forms (with .formulierSchouwen). I want to submit all forms with one button. I have read I have to use ajax.

One form has two signature pads, these signatures need to be saved in the database, so I convert them to URL and put them in two hidden fields before submitting the forms.

Then to submit the forms I use $.post and i use .then() to have a callback function after al the forms are posted.

This is the code that I put together, but I have a feeling there should be an easier/cleaner way. Can you have a look and tell me how to wright better code (if possible).

The button to submit all forms:

<button class="btn btn-primary versturen" data-action="save-svg"><strong>Verstuur</strong></button>

The js:

saveSVGButton.addEventListener("click", function (event) {
   var dataURL = signaturePad.toDataURL('image/svg+xml');    
    $("#handtekeningSchouw").val(dataURL);
//     console.log(dataURL);
//     download(dataURL, "handtekeningSchouw.svg");
    var dataURL2 = signaturePad2.toDataURL('image/svg+xml');
//     download(dataURL2, "handtekeningSchouwer.svg");
    $("#handtekeningGegevensuitwisseling").val(dataURL2);
//     console.log(dataURL2);    

$.when(
    $('.formulierSchouwen').each(function() {
    var that = $(this);
    $.post(that.attr('action'), that.serialize());
    console.log(that.serialize())
    })).then(function(){
    alert('De formulieren zijn verstuurd!');
    location.reload();
}); 

});
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301946 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO