How to check form submission in PHP ?

01 Aug, 2021
Given a form and the task is to check form submitted successfully or not. Use the PHP program for the backend to check form submission and HTML and CSS to create the form as frontend.
Syntax:
if (!empty($_POST))
if (isset($_POST['submit']))
Use these two statements to check whether the form submitted successfully or not.
Note: To run this program use localhost server like WAMP or XAMPP.
Method-1:Step 1: Create a simple form using HTML and CSS.
In this step, make an html form first to check whether it is submitted properly or not. So it is just basic programming to make a form using form tag. Use the action attribute to send the request to another PHP file. The method is used to GET the information or POST the information as entered in the form.
<!DOCTYPE html><html>    <head>        <title>Form</title>    </head>      <body>        <form action = "get-method.php" method = "post">                          Author : <input type = "text" name = "author"               placeholder = "Author's Name" />                              <br><br>                          Number of published Article : <input type = "number"              name = "num_article" placeholder = "Published Article" />                              <br><br>                          <input type = "submit" name = "submit" value = "Submit">        </form>    </body></html>                    
Output:
Step 2: Create PHP file for POST method
Use isset() method in PHP to test the form is submitted successfully or not. In the code, use isset() function to check $_POST[‘submit’] method. Remember in place of submit define the name of submit button. After clicking on submit button this action will work as POST method.
<?php  if (isset($_POST['submit'])) {    echo "GeeksforGeeks";}?>
Final output on the localhost:
Method 2:Without using separate PHP file: Without using a separate file, written an HTML code in which to specify the code for PHP action in script tag as we did in this demonstration. Both of these codes display the same output but their implementation is different.
Example:
<!DOCTYPE html><html>    <head>        <title>Form</title>    </head>       <body>                  <!-- form tag to create form -->        <form action = "get-method.php" method = "post">                          Author : <input type = "text" name = "author"                placeholder = "Author's Name" />                              <br><br>                          Number of published Article : <input type = "number"                 name="num_article" placeholder="Published Article"/>                          <br><br>                          <input type = "submit" name = "submit" value = "Submit">        </form>                  <!-- script to check form submission -->        <script>            if (isset($_POST['submit'])) {                echo "GeeksforGeeks";            }        </script>    </body></html>
Output on the localhost:
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Picked
PHP
PHP Programs
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
Comparing two dates in PHP
Split a comma delimited string into an array in PHP
How to get parameters from a URL string in PHP?
How to convert array to string in PHP ?
Comparing two dates in PHP
Split a comma delimited string into an array in PHP
How to get parameters from a URL string in PHP?
How to fetch data from localserver database and display on HTML table using PHP ?