[jQuery] jQuery.ajax custom parameter to success callback function

[jQuery] jQuery.ajax custom parameter to success callback function


Hello friends,
I am trying to validate a form using jQuery and PHP. The actual
filtering is done by a PHP class which receives the data
asynchronously from jQuery and if it finds the form field data
invalid, returns an error.
So, I am serializing the form and then split it into array. I'm
looping through the array and passing each value to the jQuery.ajax
function. I would need to return an error to the ID hook if something
is wrong and I'm stuck in creating it. PHP successfully returns the
information but I'm having a very hard time with the darn hook. Please
see the code below:
$(document).ready(function() {
$(function() {
$("#form-submit").click(function() {
/*
serialize the form and create the initial array
*/
array = $("#theform").serialize().split("&");
for(var i in array) {
/*
for each value, split it again and grab the element
key
which will be my CUSTOM_PARAMETER
*/
arr = array[i].split("=");
var custom_parameter = arr[0];
/*
arr[0] is the key which is basically the
CUSTOM_PARAMETER I need
arr[1] is the actual value and makes no interest
*/
$.ajax({
url: 'request.php',
type: 'POST',
data: array[i],
success: function(data) {
/*
PHP returns an error if the data is invalid
but I need to define a "hook" using the
CUSTOM_PARAMETER element
ex: $("#error_phone_number").html(data);
*/
$("#error_"+custom_parameter).html(data);
}
});
}
});
});
});
I hope I succeeded to make myself as clear as possible and of course,
I would really appreciate your thoughts on this.
Thank you!