PHP CHAPTER 24

PHP Form Handling

PHP form handling means receiving and processing data submitted from an HTML form.

📘

What is it?

PHP form handling means receiving and processing data submitted from an HTML form.

💡

Why do we use it?

Forms collect names, emails, phone numbers, admission information and other user input.

⚙️

How does it work?

HTML sends the form data using GET or POST. PHP reads it using $_GET or $_POST.

🧩

Syntax

PHP Syntax
$name = $_POST['name'] ?? '';
💻

Example

PHP Example
<?php

$name = $_POST['name'] ?? '';

if ($name !== '') {
    echo "Hello, " . htmlspecialchars($name);
}

?>
🔍

Example Explained

$_POST receives submitted data. ?? provides an empty value when name was not submitted. htmlspecialchars safely displays text.
🚀

More Examples

<form method="post"> <input type="text" name="name"> <button type="submit">Submit</button> </form>

⚠️ Important Rules

Always validate and safely output submitted data.

❌ Common Mistakes

Using form fields without checking whether they exist.

🌍 Real-World Use

Admission forms, contact forms and registration systems.

📝 Practice Exercise

Create a student admission form with name, email and course.

💻 Try It Yourself

Write and run this PHP lesson inside the AIFA Coding Lab.

Open Coding Lab →