I'm doing a server-side form validation. On [submit] button clicked my code adds field _validate to the form and sends the form to the server in AJAX mode. On success form should be submitted, but it not happened.
$(document).ready(function(){
$('form[action*="#validate"]').on('submit', function(e){
var $form = $(this);
var $submit = $form.find('input[type=submit]');
var $validate = $form.find('input[name=_validate]');
if ($validate.length>0 && $validate.val()=='success') // already validated
{
//e.returnValue = true; it's deprecated
console.log('validated'); // #2
return true; // supposed form submit
}
e.preventDefault();
if ($validate.length==0)
$form.append('<input type="hidden" name="_validate" value="asking" />');
$validate = $form.find('input[name=_validate]');
var params = $form.serialize();
$.ajax({
url: $form.attr('action'),
method: $form.attr('method').toLowerCase(),
data: params
})
.then(function(response, statusText, xhrObj){
var json = JSON.parse(response);
if (json._status!='ok')
{
// writing messages
}
else
{
$validate.val('success');
$form.attr('action', $form.attr('action').replace('#validate', ''));
$form.submit(); // #1
}
});
});
});
form.submit() marked with #1 makes subsequest onsubmit call, the script prints validated (#2) but form woudn't submit - no queries in Net console, no error or warning messages.