I have two forms on one html page. Using jQuery, is it possible to have the data from both forms go into the POST data when the first is submitted?
jQuery serialize supports multiple form elements, So it is possible to do:
$('#form1, #form2').serialize();
And for your case, you can do:
$('#form1').submit(function() {
var action = $(this).attr('action');
if (!EntryCheck()) return false;
$.ajax({
url : action,
type : 'POST',
data : $('#form1, #form2').serialize(),
success : function() {
window.location.replace(action);
}
});
return false;
});
Lachlan Roche's solution only copies the elements, but not the values. This will take care of values as well, and can be used to handle either form submission:
<script type="text/javascript">
var submitter = {
combine: function (form1, form2) {
$('#' + form1 + ' :input[isacopy]').remove();
$('#' + form2 + ' :input').not(':submit').not('textarea').not('select').each(function() { $(this).attr('value', $(this).val()); });
$('#' + form2 + ' textarea').each(function() { $(this).text($(this).val()); });
$('#' + form2 + ' select').each(function() { $('option[value!="' + $(this).val() + '"]', this).remove(); $('option[value="' + $(this).val() + '"]', this).attr('selected', 'selected'); });
$('#' + form2 + ' :input').not(':submit').clone().hide().attr('isacopy','y').appendTo('#' + form1);
return true;
}
};
</script>
And your form tags would look something like (notice the form ids passed to the function are switched):
<form name="my_first_form" id="my_first_form" method="post" onsubmit="if (!submitter.combine('my_first_form', 'my_second_form')) { return false; }">
...
<form name="my_second_form" id="my_second_form" method="post" onsubmit="if (!submitter.combine('my_second_form', 'my_first_form')) { return false; }">
Form validation can fit in there wherever you like - it would make most sense if your validator was another function of the submitter object, or vice versa.
I used below code to submit two forms' data in my website.
The idea is that you get the multiple forms data using serialize
and combine that data and equalize that to data
parameter of the $.ajax
function.
.
// submits two forms simultaneously
function submit_forms(form1_id, form2_id)
{
var frm1_name = $("#" + form1_id).attr('name');
var frm2_name = $("#" + form2_id).attr('name');
if (frm1_name == frm2_name)
{
alert('The two forms can not have the same name !!');
}
else
{
var frm1_data = $("#" + form1_id).serialize();
var frm2_data = $("#" + form2_id).serialize();
if (frm1_data && frm2_data)
{
$("#div_busy").html('<strong>Processing...</strong><br /><img id="busy" src="./images/progress_bar.gif" border="0" style="display:none;" />');
$("#busy").fadeIn('slow');
$.ajax(
{
type: "POST",
url: "process_sticker_request.php",
data: frm1_data + "&" + frm2_data,
cache: false,
error: function()
{
$("#busy").hide('slow');
$("#div_busy").css({'color':'#ff0000', 'font-weight':'bold'});
$("#div_busy").html('Request Error!!');
},
success: function(response)
{
$("#div_busy").hide('slow');
$("#hdnFormsData").html(response);
// open popup now with retrieved data
window.open('', 'popup2', 'toolbars = 1, resizable=1, scrollbars=1, menubar=1');
document.getElementById("prt").action = 'win_sticker.php';
document.getElementById("prt").target = 'popup2';
document.getElementById("prt").submit();
// reset the action of the form
document.getElementById("prt").action = 'list_preview.php';
}
});
}
else
{
alert('Could not submit the forms !!');
}
}
}