Reputation: 663
I have two different arrays namely main_name[] and sub_name[]. I have to pass these values to the next page using AJAX, for inserting into DB.Could you please help me out, how could I pass the array as data in AJAX. HTML
<input type="text" name = "main_name[]" value = "" >
<input type="text" name = "sub_name[]" value = "" >
JS
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$(".the-return").html
);
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});
Upvotes: 3
Views: 1255
Reputation: 15772
You could simply use serializeArray
var data = $(form).serializeArray();
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function (data) {
$(".the-return").html();
}
});
Upvotes: 2
Reputation: 869
$('form').serialize()
will serialize all your form elements together with values and send along with an ajax query.
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: $('#form_id').serialize(),
success: function(data) {
$(".the-return").html
);
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});
Upvotes: 2
Reputation: 6755
try this it will get all values of your form in (data) variable and send through ajax in json format.
var data = $('form').serialize();
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$(".the-return").html
);
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});
Upvotes: 1