How to submit a form without using submit button using PHP ?
In this article, we will see how to submit the form without clicking the submit button, along with understanding the different ways to accomplish this task through implementation.
Example 1: This example illustrates how to submit the form without a submit button and store the data in a database using phpMyAdmin.
Note: <input> with attribute type=”submit” will not be submitted with the form while using this method.
form.php: This code will help us to connect PHP with the database and store data submitted by the user.
Explanation: Here, we need to add the data filled in the form to the database without using submit button. For this, we need to trigger submit() function using HTML element. Using the getElementById() method, we have targeted the required form to be submitted. As soon as HTML is clicked submit() is executed and data is stored in the database. In the above example, we have used the <h5> tag to trigger submit function.
The text typed will be stored in the database after clicking the HTML element as shown in the gif.
Example 2: This example describes how to use an image to submit the form. Here, we have used <img> tag to trigger submit() function. Here, we will use the form.php file to store data submitted by the user.
Explanation: In above both the examples, as soon as the HTML element is clicked, ‘onclick‘ event is triggered by targeting element using getElementById() method.Then form.submit() function is executed to submit the form.