How to Check form Submission in PHP?

To check form submission in PHP, you can use several methods to determine if a form has been submitted and process the data accordingly. The most common approach is checking the request method or specific form fields.

Method 1: Checking Request Method

The most reliable way is to check if the request method is POST ?

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    echo "Form was submitted via POST method";
    // Process form data here
} else {
    echo "No form submission detected";
}
?>

Method 2: Using isset() Function

You can check if a specific form field or submit button exists ?

<?php
if (isset($_POST['submit'])) {
    echo "Form submitted using submit button";
    // Process form data
} elseif (isset($_POST['email'])) {
    echo "Form submitted with email field";
    // Process form data
} else {
    echo "Form not submitted";
}
?>

Complete Example

HTML Form (index.html)

<!DOCTYPE html>
<html>
<head>
    <title>Employee Information Form</title>
</head>
<body>
    <h2>Employee Information</h2>
    <form action="process_form.php" method="POST">
        <label for="fname">First Name:</label>
        <input type="text" id="fname" name="fname" required><br><br>
        
        <label for="lname">Last Name:</label>
        <input type="text" id="lname" name="lname" required><br><br>
        
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required><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>
        </select><br><br>
        
        <button type="submit" name="submit">Submit</button>
    </form>
</body>
</html>

PHP Processing (process_form.php)

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Validate and sanitize input data
    $fname = htmlspecialchars($_POST["fname"]);
    $lname = htmlspecialchars($_POST["lname"]);
    $email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
    $department = htmlspecialchars($_POST["department"]);
    
    // Display submitted data
    echo "<h2>Form Submitted Successfully</h2>";
    echo "First Name: " . $fname . "<br>";
    echo "Last Name: " . $lname . "<br>";
    echo "Email: " . $email . "<br>";
    echo "Department: " . $department . "<br>";
} else {
    echo "No form data received. Please submit the form.";
}
?>

Validation Methods

Method Purpose Example
$_SERVER["REQUEST_METHOD"] Check HTTP method POST, GET
isset() Check if variable exists isset($_POST['field'])
!empty() Check if field has value !empty($_POST['field'])
Note: To run this example, you need a web server with PHP support. Save the HTML file as index.html and PHP file as process_form.php in the same directory.

Conclusion

Checking form submission in PHP involves verifying the request method using $_SERVER["REQUEST_METHOD"] or using isset() to check specific form fields. Always sanitize and validate user input for security, and implement proper error handling for robust form processing.

Updated on: 2026-03-15T10:27:48+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements