I have an issue I can't seem to solve. First some context that may be relevant.
I am building a MVC-like (I say "like" because I'm still learning how MVC works) php framework for my software. I want all the input forms to submit data via ajax to a backend controller that routes the data and inputs it into the appropriate database. All the HTML code is generated through php classes and the JavaScript in included through php classes.
The problem I'm having is with the ajax function. It keeps trying to submit the post request normally while I want it to submit asynchronously. I've had it working properly at various points in time but it seems to break when I change aspects of the framework. I've tried various combinations of "form.preventDefault()" and "return false" but it doesn't seem to matter. The validation part works (it won't submit with invalid entries. Using this plugin). I discovered that JavaScript caches itself and I've worked through that by appending version numbers to the file name.
Here is the ajax code. It returns a message to the user about the status of the request. Any idea why it refuses to submit asynchronously?
$(document).ready(function() {
// Initialize form validation
***old code
$("form[name='ajaxform']").validate({ ***
// Specify validation rules
***Updated code
$('#ajaxsubmit').on('click', function() {
$("#ajaxform").valid();
});
// Initialize form validation
$('#ajaxform').validate({ ***
....[omitting the validation rules for brevity]
errorElement : 'div',
errorLabelContainer: '#usermessage',
submitHandler: function(form) {
$.ajax({
cache : false,
url : $(form).attr('action'),
type : 'POST',
dataType: 'text',
data : $(form).serialize(),
success : function(data) {
var response = $.parseJSON(data);
if(response['error']){
$('#usermessage').html(response['error']);
}else{
if(response['success']){
$('#usermessage').css({"color":"#00b300","font-weight":"bold"});
$('#usermessage').html(response['success']);
}
}
},
error : function(err) {
alert(err);
}
});
return false;
}
});
});