tengxuanp
tengxuanp

Reputation: 179

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!

Upvotes: 0

Views: 1174

Answers (1)

Lukaaashek
Lukaaashek

Reputation: 56

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

Upvotes: 3

Related Questions