PHP Practical Slip 19 Q.B

Write a PHP program to accept name, address, pincode, gender information. If any
field is blank display error messages “all fields are required”.

<?php

$error = "";
$name = $address = $pincode = $gender = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $name = trim($_POST["name"]);
    $address = trim($_POST["address"]);
    $pincode = trim($_POST["pincode"]);
    $gender = isset($_POST["gender"]) ? $_POST["gender"] : "";

    // Check if any field is empty
    if (empty($name) || empty($address) || empty($pincode) || empty($gender)) {
        $error = "All fields are required";
    } else {
        echo "<h3>Submitted Information</h3>";
        echo "Name: $name <br>";
        echo "Address: $address <br>";
        echo "Pincode: $pincode <br>";
        echo "Gender: $gender <br>";
        exit();
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Registration Form</title>
</head>
<body>

<h2>User Information Form</h2>

<?php
if (!empty($error)) {
    echo "<p style='color:red;'>$error</p>";
}
?>

<form method="post" action="">
    Name: <input type="text" name="name"><br><br>
    Address: <input type="text" name="address"><br><br>
    Pincode: <input type="text" name="pincode"><br><br>
    
    Gender:
    <input type="radio" name="gender" value="Male"> Male
    <input type="radio" name="gender" value="Female"> Female
    <br><br>

    <input type="submit" value="Submit">
</form>

</body>
</html>
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top