$(document).ready(function () { $('#submit_form').on("click", function() { var name = $('#form_name').val(); var company = $('#form_company').val(); var tel = $('#form_tel').val(); var email = $('#form_email').val(); var text = $('#form_text').val(); $('#successAlert').hide(1000); $('#failAlert').hide(1000); var success = true; if(name.length == 0) { $('#form_name').css("background-color", "#ffaaaa"); success = false; } else { $('#form_name').css("background-color", "#fff"); } if(company.length == 0) { $('#form_company').css("background-color", "#ffaaaa"); success = false; } else { $('#form_company').css("background-color", "#fff"); } if(tel.length == 0) { $('#form_tel').css("background-color", "#ffaaaa"); success = false; } else { $('#form_tel').css("background-color", "#fff"); } if(email.length == 0) { $('#form_email').css("background-color", "#ffaaaa"); success = false; } else { $('#form_email').css("background-color", "#fff"); } if(text.length == 0) { $('#form_text').css("background-color", "#ffaaaa"); success = false; } else { $('#form_text').css("background-color", "#fff"); } if(success==true) { console.log('sending'); var message = { name: name, company:company, tel:tel, email:email, text:text } $.ajax({ type:'POST', url: '/api/mail/send', processData: false, // tell jQuery not to process the data contentType: false, // tell jQuery not to set contentType data: JSON.stringify(message), success: (data) => { if(data=='True'){ $('#successAlert').show(1000); } else { $('#failAlert').show(1000); } } }); $('#callback_form')[0].reset(); } }); });