Sorry, but nothing matched your search terms. Please try again with some different keywords.
One JS code to conquer all forms!
Javascript is the glue for any web application that connects the UI and the back end, making an application less annoying with unwanted submits and refreshes. This also acts as an agent who handles the message passing in between the front-end and the back-end.
Javascript is cool and all, but too much javascript is not cool. If you remember your first days at computer school, it was one thing your tutors would keep harping on, “Do the job with less code as much as possible!”. Okay fair enough! this usually takes about more than a couple of years for a novice programmer to actually do that. lol. Writing efficient code is not taught, it is learned. And that needs a lot of dedication and hardworking. Something a none techie would “NOT” I repeat “NOT” understand at all.
So recently, I was working on some applications that had to include a massive amount of web forms. and thinking ahead that I have to handle AJAX requests to pass messages to the back-end for all the forms individually, decided to break the norm. After all, there aren’t any rules for coding apart from the house rules your senior asks you to follow.
So let’s get on with it.
/*
When you see the magic $, you know you need Jquery for this.
I'm demoing this on a laravel framework. If you decide not to CSRF, the following block of ajaxSetup isn't required.
*/
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$(document).ready(function () {
/*
This will tap all the form submissions and carry forward.
*/
$('form').submit(function (e) {
e.preventDefault();
var routeData = getRoute(e.target.id);
if (routeData) {
var data = new FormData(this);
ajax(data, routeData['route']);
} else {
console.error('Route not defined for this form ID : ' + e.target.id);
}
});
/*
Have used a swich to define which route to be sent to call at each form
ID. For some reason I chose to save parameters in an array and I dont
remember why =( . Btw, more the forms, more the cases.
*/
function getRoute(formId) {
var data = [];
switch (formId) {
case 'frmCreateGaCredentials':
data['route'] = "ga/analytics-create";
return data;
default:
return false;
}
}
/*
Ajax call to the back-end. This always sends the form
as a JS object. You can do the modifications if you wish to
pass data as a serialized form.
*/
function ajax(formData, route, refresh = false) {
$.ajax({
url: base + route,
type: 'post',
data: formData,
processData: false,
contentType: false,
success: function (respond) {
if (respond.success) {
// Console the success or propose a toast
console.log(respond.data);
} else {
// Not a success, maybe again propose a toast =D
alert(respond.data);
if (refresh) {
location.reload();
}
}
},
complete: function () {
}
});
}
});
So as shown this javascript will handle all the form submissions and call the relevant route and get back the responses, will play a great deal saving time for unnecessary ajax request handles.