Home > Web Front-end > JS Tutorial > How to Submit Two HTML Forms Simultaneously with One Button?

How to Submit Two HTML Forms Simultaneously with One Button?

Barbara Streisand
Release: 2024-12-02 05:58:17
Original
355 people have browsed it

How to Submit Two HTML Forms Simultaneously with One Button?

How to Submit Two Forms with One Button

In the HTML document, it is possible to have multiple forms with different actions. However, submitting them both individually can be inconvenient for the user. This article explores a solution to this dilemma by utilizing JavaScript to submit both forms simultaneously.

First Form: Submitting Data to the Database

The first form, updateDB, is responsible for sending data to a database using PHP. Here's a sample HTML code:

<form>
Copy after login
Copy after login

Second Form: Redirecting to PayPal Payment Page

The second form, payPal, directs the user to a payment page upon submission.

<form>
Copy after login
Copy after login

Using JavaScript to Submit Both Forms

To submit both forms simultaneously, follow these steps:

  1. Create a JavaScript function to handle the submit event.
  2. In the function, use XMLHttpRequest or fetch() to submit the updateDB form asynchronously.
  3. After the updateDB form is submitted successfully, submit the payPal form.
function submitBothForms() {
    // Submit updateDB form asynchronously via XMLHttpRequest
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "mypage.php", true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send(postData.join("&"));

    // Submit payPal form
    document.getElementById("payPal").submit();
}
Copy after login

Updated Code with Fetch()

async function submitBothForms() {
    // Submit updateDB form asynchronously via fetch()
    const res = await fetch("mypage.php", {
        method: "POST",
        headers: { "content-type": "application/x-www-form-urlencoded" },
        body: new FormData(updateDB),
    });
    if (!res.ok) throw new Error("DB Update Failed!");

    // Submit payPal form
    payPal.submit();
}
Copy after login

Conclusion

By leveraging JavaScript, it is feasible to submit two forms with a single button. This approach provides a seamless experience for users, avoiding the need for multiple submissions. Remember to tailor the error handling and messaging based on your specific requirements.

The above is the detailed content of How to Submit Two HTML Forms Simultaneously with One Button?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template