Home > Web Front-end > JS Tutorial > body text

Submit r More forms in one button click

PHPz
Release: 2024-07-31 14:39:52
Original
486 people have browsed it

Submit r More forms in one button click

Suppose you have two from on your web page in different places. but you want to submit two forms in one click. Then use -

Your HTML:

<form id="form1">
   <!-- Form 1 fields -->
   <input type="text" name="field1" placeholder="Field 1">
</form>

<form id="form2">
    <!-- Form 2 fields -->
    <input type="text" name="field2" placeholder="Field 2">
</form>
Copy after login

JS:

window.addEventListener('load', function() {
            // Get form data
            let form1 = new FormData(document.getElementById('form1'));
            let form2 = new FormData(document.getElementById('form2'));

            // Combine both forms data into URLSearchParams
            let urlParams = new URLSearchParams();
            form1.forEach((value, key) => {
                urlParams.append(key, value);
            });
            form2.forEach((value, key) => {
                urlParams.append(key, value);
            });

            // Redirect to a new URL with combined form data
            window.location.href = '/submit-both-forms?' + urlParams.toString();
        });
Copy after login

This will send a get request to the location.

The above is the detailed content of Submit r More forms in one button click. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!