I have my code validating the necessary data, but I want to manage the submit based on the response from AJAX. The response is an array of boolean values, for any false value, I want to prevent the form from submitting. I was looking at this solution here, but I would prefer not using a plugin, if possible.
$(document).ready( function () {
console.log('ready');
var submitButton = $('#tagProductForm');
submitButton.on('submit', function (e) {
e.preventDefault();
var tags = ($('.tag').map(function () { return $(this).val(); })).get();
$.ajax({
type: "POST",
url: "validateTag.php",
data: 'jData=' + JSON.stringify(tags)
})
.done(function( response ) {
response = response.replace(/[[\]]/g,'');
var responseArray = response.split(",");
var size = responseArray.length;
var valid = true;
if(size != 0){
for(var i = 0; i < size; ++i){
if(responseArray[i] == 'false'){
$('#tag' + i).attr('style','border:3px solid #FF0000');
// set valid to false
}
}
// if valid is false, prevent form from submitting
}
})
.fail(function() {
$('#response').html('error');
})
});
});