Merge values from two forms on submit

前端 未结 9 903
甜味超标
甜味超标 2020-11-29 18:12

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?

9条回答
  •  攒了一身酷
    2020-11-29 18:57

    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('Processing...
    '); $("#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 !!'); } } }

提交回复
热议问题