Submit HTML form, two actions one button

tengxuanp :

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!

Lukaaashek :

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;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=302494&siteId=1