简体   繁体   中英

Submit HTML form, two actions one button

this is my form:

<form id="contact-form" method="post">

and I have to pass the data inside to two URLs by clicking this button:

<button onclick="sendButton();" type="submit" name="submit" class="btn btn-primary btn-lg  login-button cont-submit center-block"><i class="fa fa-paper-plane" aria-hidden="true"></i> Send Request</button>

the button will trigger the javascript below:

function sendButton()
{
    document.forms['contact-form'].action='excelReports.php';
    document.forms['contact-form'].target='';
    document.forms['contact-form'].submit();

    document.forms['contact-form'].action='mailAnnual.php';
    document.forms['contact-form'].target='';
    document.forms['contact-form'].submit();
    return true;
}

Unfortunately, the javascript didn't help to pass the data successfully, I wonder if anyone knows the problem. Thank you!

Second submit will be never executed. You must use AJAX. For example:

function sendButton()
{
    var form = document.getElementById('contact-form');
    var formData = new FormData(form);

    var xhr1 = new XMLHttpRequest();
    xhr1.open('POST', 'excelReports.php', true);
    xhr1.send(formData);

    var xhr2 = new XMLHttpRequest();
    xhr2.open('POST', 'mailAnnual.php', true);
    xhr2.send(formData);

    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM