Usually, the form is sent when the user presses the submit button. However, in some cases, you may need to submit the form formally using JavaScript. JavaScript provides a form factor that contains the submit () method. Use the form 'id' to find the form object.


#index.php

<!DOCTYPE html>
<html>
    <body>
        <p>Enter data and submit the form.</p>
        <form id="myForm" action="post.php">
            First name: <input type="text" name="fname" class="form-control"><br>
            Last name: <input type="text" name="lname" class="form-control"><br><br>
            <input type="button" onclick="submitForm()" value="Submit form">
        </form>

        <script>
            function submitForm() {
                document.getElementById(“myForm").submit();

            }
        </script>
    </body>
</html>

Run the following code and see the results