jquery; my serialization of a form as a JSON object to be sent via Ajax to PHP script does not work
in Using jQuery
•
8 years ago
i am NEW to jquery and still trying to get to grips with it.
i am trying to send a form to a PHP script via Ajax using JSON. my form is not being processed and i have no idea how to trace where the problem is; i.e debug in jquery.
please note that when i complete my form and attenpt to send it, i get the following message:
processing.
this is the messages that is included in my form below, its means that the form validated. it is the next part of the form that does not work. i.e the part where the form is meant to be serilized.
please note that when i complete my form and attenpt to send it, i get the following message:
processing.
this is the messages that is included in my form below, its means that the form validated. it is the next part of the form that does not work. i.e the part where the form is meant to be serilized.
so any help and advice would be greatly appriciated.
i enclose below my code.
please note that i have had to serialize the form so that it can be used as an object for JSON. i have used the following plugin to serialize the form.
//START OF FUNCTIONS TO SERILIZE THE FORM SO THAT IT MAY BE SUBMITTED BY JSON.
(function($) {
$.fn.serializeFormJSON = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
})(jQuery);
BELOW IS MY FUCNTION TO RECEIVE THE DATA FROM THE FORMS
$(document).ready(function() {
//START THE FUNCTION FOR THE SUBMISSION OF REGISTATION FORM TO THE DATABASE VIA AJAX
$('form #response').hide();
$('#submit').click(function(e) {
// prevent forms default action until
// error check has been performed
e.preventDefault();
// grab form field values
var valid = '';
var required = ' is required.';
var name = $('form #name').val();
var email = $('form #email').val();
var cat = $('form #cat option:selected').val();
var location = $('form #country_loc option:selected').val();
var honeypot = $('form #honeypot').val();
var humancheck = $('form #humancheck').val();
// perform error checking
if (name = '' || name.length <= 2 || name == 'name*' ) {
valid = '<p>Your name' + required +'</p>';
}
if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
valid += '<p>Your email' + required +'</p>';
}
if (cat = '' || cat.length < 1) {
valid += '<p>Please tell us whether you are; seeking work or looking for a careworker' + required + '</p>';
}
if (location = '' || location.length < 1) {
valid += '<p>Please tell us your current location' + required + '</p>';
}
if (honeypot != 'http://') {
valid += '<p>Spambots are not allowed.</p>';
}
if (humancheck != '') {
valid += '<p>A human user' + required + '</p>';
}
// let the user know if there are erros with the form
if (valid != '') {
$('form #response').removeClass().addClass('error')
.html('<strong>Please correct the errors below.</strong>' +valid).fadeIn('fast');
}
// let the user know something is happening behind the scenes
// serialize the form data and send to our ajax function
else {
$('form #response').removeClass().addClass('processing').html('Processing...').fadeIn('fast');
var formData = $('form').serializeFormJSON();
submitForm(formData);
}
});
//END THE FUNCTION FOR THE SUBMISSION OF REGISTATION FORM TO THE DATABASE VIA AJAX
});//end ready fucntion.
THE ABOVE IS THE FIRST PART OF THE FORM. this part work because when i submit the form, i get the message that the form is being procecess. its the next part of teh script taht is not working.
I NOW ENCLOSE BELOW THE AJAX FUNCITON.
// make our ajax request to the server function submitForm(formData) {
$.ajax({
type: 'POST',
url: 'cms/index.php?view=joint_reg',
data: formData,
dataType: 'json',
cache: false,
timeout: 7000,
success: function(data) {
$('form #response').removeClass().addClass((data.error === true) ? 'error' : 'success')
.html(data.msg).fadeIn('fast');
if ($('form #response').hasClass('success')) {
setTimeout("$('form #response').fadeOut('fast')", 5000);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('form #response').removeClass().addClass('error')
.html('<p>There was an<strong> ' + errorThrown +
'</strong> error due to a<strong> ' + textStatus +
'</strong> condition.</p>').fadeIn('fast');
},
complete: function(XMLHttpRequest, status) {
$('form')[0].reset();
}
});
};//END SUBMIT FORM VIA AJAX, USING JSON
THANK YOU IN ADVANCE FOR YOUR KIND ATTENTION.
WARMEST REGARDS
ANDREEA
1