PHP Form Required Fields
Making form fields mandatory for submission
⚠️ What are Required Fields?
Required fields are form inputs that must be filled before submission. They ensure users provide essential information, preventing incomplete data from being processed in your application.
<?php
// Check if field is empty
if (empty($_POST['username'])) {
echo "Username is required!";
}
?>
Required Field Techniques
Empty Check
Verify field has content
<?php
if (empty($name)) {
$error = "Required";
}
?>
Isset Check
Check if variable is set
<?php
if (!isset($_POST['email'])) {
$error = "Email missing";
}
?>
Trim & Check
Remove spaces before checking
<?php
$name = trim($_POST['name']);
if ($name == "") {
$error = "Required";
}
?>
HTML5 Required
Browser-side validation
<input type="text"
name="name"
required>
🔹 Basic Required Field Validation
Simple example checking if a name field is filled:
<?php
$name = "";
$nameErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = $_POST["name"];
echo "Welcome, " . htmlspecialchars($name);
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input type="text" name="name">
<span style="color:red;">* <?php echo $nameErr; ?></span>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
🔹 Multiple Required Fields
Validating several required fields in a registration form:
<?php
$errors = array();
$name = $email = $password = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check name
if (empty($_POST["name"])) {
$errors['name'] = "Name is required";
} else {
$name = trim($_POST["name"]);
}
// Check email
if (empty($_POST["email"])) {
$errors['email'] = "Email is required";
} else {
$email = trim($_POST["email"]);
}
// Check password
if (empty($_POST["password"])) {
$errors['password'] = "Password is required";
} else {
$password = $_POST["password"];
}
// If no errors, process form
if (empty($errors)) {
echo "Registration successful!";
}
}
?>
<form method="post">
Name: <input type="text" name="name" value="<?php echo $name; ?>">
<span style="color:red;">* <?php echo $errors['name'] ?? ''; ?></span>
<br><br>
Email: <input type="text" name="email" value="<?php echo $email; ?>">
<span style="color:red;">* <?php echo $errors['email'] ?? ''; ?></span>
<br><br>
Password: <input type="password" name="password">
<span style="color:red;">* <?php echo $errors['password'] ?? ''; ?></span>
<br><br>
<input type="submit" value="Register">
</form>
🔹 Required with Whitespace Check
Prevent users from submitting only spaces or empty strings:
<?php
function isRequired($field) {
// Trim whitespace and check if empty
$value = trim($field);
return !empty($value);
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = $_POST["username"] ?? "";
if (!isRequired($username)) {
echo "Username is required and cannot be just spaces";
} else {
echo "Username accepted: " . htmlspecialchars($username);
}
}
?>
<form method="post">
Username: <input type="text" name="username">
<input type="submit" value="Submit">
</form>
🔹 Combining HTML5 and PHP Validation
Use both client-side and server-side validation for better security:
<?php
$email = "";
$emailErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Server-side validation (always required!)
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = $_POST["email"];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
}
?>
<!-- HTML5 required attribute provides client-side validation -->
<form method="post">
Email: <input type="email" name="email" required>
<span style="color:red;">* <?php echo $emailErr; ?></span>
<br><br>
<input type="submit" value="Submit">
</form>
Important: Always validate on the server-side! HTML5 validation can be bypassed by users, so PHP validation is essential for security.
🔹 Required Field Indicator
Show users which fields are mandatory with visual indicators:
<!DOCTYPE html>
<html>
<head>
<style>
.required {color: red;}
.error {color: red; font-size: 0.9em;}
label {display: inline-block; width: 100px;}
</style>
</head>
<body>
<h2>Contact Form</h2>
<p><span class="required">* Required fields</span></p>
<form method="post">
<label>Name: <span class="required">*</span></label>
<input type="text" name="name">
<span class="error"><?php echo $nameErr ?? ''; ?></span>
<br><br>
<label>Email: <span class="required">*</span></label>
<input type="email" name="email">
<span class="error"><?php echo $emailErr ?? ''; ?></span>
<br><br>
<label>Phone:</label>
<input type="tel" name="phone">
<span style="font-size: 0.9em;">(Optional)</span>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
🔹 Array of Required Fields
Efficiently validate multiple required fields using an array:
<?php
$required_fields = ['name', 'email', 'phone', 'message'];
$errors = [];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
foreach ($required_fields as $field) {
if (empty(trim($_POST[$field] ?? ''))) {
$errors[$field] = ucfirst($field) . " is required";
}
}
if (empty($errors)) {
echo "All required fields are filled!";
}
}
?>
<form method="post">
<?php foreach ($required_fields as $field): ?>
<label><?php echo ucfirst($field); ?>: *</label>
<input type="text" name="<?php echo $field; ?>">
<span style="color:red;"><?php echo $errors[$field] ?? ''; ?></span>
<br><br>
<?php endforeach; ?>
<input type="submit" value="Submit">
</form>