How to Check form Submission in PHP?


How to check form submission in PHP?

To check form submission in PHP, you can follow these steps:

Step 1

Create an HTML form: Start by creating an HTML form that collects user input. The form should have an action attribute pointing to the PHP file where you will process the form data.

Index.html

<!DOCTYPE html>
<html>
<head>
   <title>Employee Information Form</title>
   <style>
   .error {
     color: red;
   }
  </style>
</head>
<body>
<h2>Employee Information</h2>
<form name="process_form" action="process_form.php" 
method="POST" onsubmit="return validateForm()">
   <label for="fname">First Name:</label>
   <input type="text" id="fname" name="fname" 
required>
   <br>
   <br><br>
   <label for="lname">Last Name:</label>
   <input type="text" id="lname" name="lname" 
required>
   <br>
   <br><br>
   <label for="email">Email:</label>
   <input type="email" id="email" name="email" 
required>
   <br>
   <br><br>
   <label for="phone">Phone:</label>
   <input type="text" id="phone" name="phone" 
required>
   <br>
   <br><br>
   <label for="department">Department:</label>
   <select id="department" name="department" required>
      <option value="">Select Department<
/option>
      <option value="HR">Human Resources<
/option>
      <option value="IT">Information Technology
</option>
      <option value="Finance">Finance</option>
      <option value="Sales">Sales</option>
   </select>
   <br>
   <br><br>
   <label for="salary">Salary:</label>
   <input type="number" id="salary" name="salary" min="0" required>
   <br>
   <br><br>
   <button type="submit">Submit</button>
</form>
<script>
   function validateForm() {
     var fname = document.forms["employeeForm"]["fname"].value;
     var lname = document.forms["employeeForm"]["lname"].value;
     var email = document.forms["employeeForm"]["email"].value;
     var phone = document.forms["employeeForm"]["phone"].value;
     var department = document.forms["employeeForm"]["department"].value;
     var salary = document.forms["employeeForm"]["salary"].value;
     if (fname.trim() == "") {
      alert("First Name is required");
      return false;
     }
     if (lname.trim() == "") {
      alert("Last Name is required");
      return false;
     }
     if (email.trim() == "") {
      alert("Email is required");
      return false;
     }
     if (phone.trim() == "") {
      alert("Phone is required");
      return false;
     }
     if (department == "") {
      alert("Please select a Department");
      return false;
    }
     if (salary.trim() == "") {
      alert("Salary is required");
      return false;
     }
     return true;
   }
  </script>
</body>
</html>

Output

Before submission

Step 2

Process the form data in PHP: Create a PHP file (e.g., process_form.php) that will handle the form submission. In this file, you can check if the form was submitted using an if statement based on the presence of the form field you want to check (e.g., a submit button).

Process_form.php

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
   // Retrieve form data
   $fname = $_POST["fname"];
   $lname = $_POST["lname"];
   $email = $_POST["email"];
   $phone = $_POST["phone"];
   $department = $_POST["department"];
   $salary = $_POST["salary"];
   // Perform further operations, such as validation, database interactions, etc.
   // ...
   // Example: Display submitted data
   echo "<h2>Employee Information</h2>";
   echo "First Name: " . $fname . "<br>";
   echo "Last Name: " . $lname . "<br>";
   echo "Email: " . $email . "<br>";
   echo "Phone: " . $phone . "<br>";
   echo "Department: " . $department . "<br>";
   echo "Salary: " . $salary . "<br>";
} else {
   // Handle the case when the form is not submitted
   echo "Form was not submitted.";
}
?>

NOTE: Make sure to save the HTML form in a file named index.html and the PHP code in a file named process_form.php. Place both files in the same directory on your server.

When the user fills in the form and clicks the "Submit" button, the form data will be sent to process_form.php for processing. The PHP code will check if the form was submitted and display the submitted data.

After form submission

If the form was not submitted, it will display a message indicating that the form was not submitted.

Once if the form has any validation errors then it won't allow the form to submit.

Conclusion

To check form submission in PHP, you can use the isset() function to determine if the form's submit button or any specific form field is set. If the form is submitted, you can access the submitted data using the $_POST superglobal for POST method or $_GET for GET method. You can then perform validations, sanitize the data, and process it further as needed, such as storing it in a database, sending emails, or performing other operations. It's important to ensure proper validation and security measures are implemented to protect against vulnerabilities and ensure the integrity of the submitted data.

Updated on: 28-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements