0% found this document useful (0 votes)
3 views5 pages

Php practical no.10 outputs

The document provides a practical example of creating a registration form using HTML and PHP, including text boxes, radio buttons, checkboxes, and a submit button. It includes form validation for required fields like name and gender, as well as hobby selection. Upon successful submission, the form displays the entered details and validation messages if any fields are incomplete.

Uploaded by

ARYAN MOHADE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Php practical no.10 outputs

The document provides a practical example of creating a registration form using HTML and PHP, including text boxes, radio buttons, checkboxes, and a submit button. It includes form validation for required fields like name and gender, as well as hobby selection. Upon successful submission, the form displays the entered details and validation messages if any fields are incomplete.

Uploaded by

ARYAN MOHADE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PRACTICAL No.

10
Outputs

Q.1 Write a program to design a registration form using textbox, radio button,
checkbox and button.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Registration Form</title>
</head> Output
<body>
Registration Details:

<h2>Registration Form</h2> Name: John Doe

Gender: Male
<form method="POST" action="">
<!-- Textbox for name --> Terms: Agreed
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>

<!-- Radio buttons for gender -->


<label>Gender:</label>
<input type="radio" id="male" name="gender" value="Male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="Female">
<label for="female">Female</label><br><br>

<!-- Checkbox for terms and conditions -->


<label for="terms">I agree to the terms and conditions</label>
<input type="checkbox" id="terms" name="terms" value="Agree"><br><br>

<!-- Submit button -->


<button type="submit" name="submit">Register</button>
</form>
PRACTICAL No.10
Outputs

<?php
if (isset($_POST['submit'])) {
// Capture form data
$name = $_POST['name'];
$gender = $_POST['gender'];
$terms = isset($_POST['terms']) ? "Agreed" : "Not Agreed";

// Display the submitted data


echo "<h3>Registration Details:</h3>";
echo "Name: " . htmlspecialchars($name) . "<br>";
echo "Gender: " . htmlspecialchars($gender) . "<br>";
echo "Terms: " . $terms . "<br>";
}
?>
</body>
</html>

Q. Develop web page and do validation using control text box, radio button , check
box and button.

Ans.

<?php
$nameErr = $genderErr = $hobbyErr = "";
$name = $gender = $hobby = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Name validation
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
PRACTICAL No.10
Outputs

// Gender validation
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}

// Hobby validation
if (empty($_POST["hobby"])) {
$hobbyErr = "At least one hobby must be selected";
} else {
$hobby = test_input($_POST["hobby"]);
}
}

function test_input($data) { Output


$data = trim($data);
$data = stripslashes($data); Form Submitted Successfully!
$data = htmlspecialchars($data);
return $data;
Name: John
} Gender: Male
?>
Hobbies: Reading, Gaming
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Form Validation</title>
</head>
<body>
PRACTICAL No.10
Outputs

<h2>PHP Form Validation Example</h2>


<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<!-- Name Input -->
Name: <input type="text" name="name" value="<?php echo $name; ?>">
<span style="color: red;"><?php echo $nameErr;?></span>
<br><br>

<!-- Gender Radio Buttons -->


Gender:
<input type="radio" name="gender" value="Male" <?php if ($gender ==
"Male") echo "checked"; ?>> Male
<input type="radio" name="gender" value="Female" <?php if ($gender ==
"Female") echo "checked"; ?>> Female
<span style="color: red;"><?php echo $genderErr;?></span>
<br><br>

<!-- Hobbies Checkboxes -->


Hobbies:
<input type="checkbox" name="hobby[]" value="Reading" <?php if
(in_array("Reading", $hobby)) echo "checked"; ?>> Reading
<input type="checkbox" name="hobby[]" value="Traveling" <?php if
(in_array("Traveling", $hobby)) echo "checked"; ?>> Traveling
<input type="checkbox" name="hobby[]" value="Gaming" <?php if
(in_array("Gaming", $hobby)) echo "checked"; ?>> Gaming
<span style="color: red;"><?php echo $hobbyErr;?></span>
<br><br>

<!-- Submit Button -->


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

<?php
PRACTICAL No.10
Outputs

if ($_SERVER["REQUEST_METHOD"] == "POST" && !$nameErr &&


!$genderErr && !$hobbyErr) {
echo "<h3>Form Submitted Successfully!</h3>";
echo "Name: $name <br>";
echo "Gender: $gender <br>";
echo "Hobbies: " . implode(", ", $hobby) . "<br>";
}
?>

</body>
</html>

You might also like