0% found this document useful (0 votes)
29 views6 pages

Web Form Validation and Registration Guide

The document contains code for two questions - developing a web page for form validation using controls like text box, radio buttons, check boxes and buttons. The second question is about designing a registration form using similar controls.

Uploaded by

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

Web Form Validation and Registration Guide

The document contains code for two questions - developing a web page for form validation using controls like text box, radio buttons, check boxes and buttons. The second question is about designing a registration form using similar controls.

Uploaded by

gmpawar003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

WBP CO6I Practical 10

Name: Ashwin Pawr Roll No: 06


Practical: 10

[Link] web page and do validation using control text box,


radio button, check box and button.
CODE:
<!DOCTYPE html>

<html>

<head>

<title>Form Validation</title>

</head>

<body>

<?php

// Define variables and set to empty values

$nameErr = $emailErr = $genderErr = "";

$name = $email = $gender = "";

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

// Validation for name

if (empty($_POST["name"])) {

$nameErr = "Name is required";

} else {

$name = test_input($_POST["name"]);

// Check if name only contains letters and whitespace

if (!preg_match("/^[a-zA-Z ]*$/", $name)) {

$nameErr = "Only letters and white space allowed";

Gramin Technical & managemnet campus 1


WBP CO6I Practical 10

// Validation for email

if (empty($_POST["email"])) {

$emailErr = "Email is required";

} else {

$email = test_input($_POST["email"]);

// Check if email is valid

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

$emailErr = "Invalid email format";

// Validation for gender

if (empty($_POST["gender"])) {

$genderErr = "Gender is required";

} else {

$gender = test_input($_POST["gender"]);

function test_input($data) {

$data = trim($data);

$data = stripslashes($data);

$data = htmlspecialchars($data);

return $data;

?>

<h2>Form Validation</h2>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

Gramin Technical & managemnet campus 2


WBP CO6I Practical 10

Name: <input type="text" name="name">

<span class="error">* <?php echo $nameErr; ?></span>

<br><br>

E-mail: <input type="text" name="email">

<span class="error">* <?php echo $emailErr; ?></span>

<br><br>

Gender:

<input type="radio" name="gender" value="female">Female

<input type="radio" name="gender" value="male">Male

<input type="radio" name="gender" value="other">Other

<span class="error">* <?php echo $genderErr; ?></span>

<br><br>

<input type="checkbox" name="agree" value="1"> I agree to the terms and conditions

<br><br>

<input type="submit" name="submit" value="Submit">

</form>

<?php

// Display submitted form data

if ($_SERVER["REQUEST_METHOD"] == "POST" && empty($nameErr) && empty($emailErr) &&


empty($genderErr)) {

echo "<h2>Submitted Information:</h2>";

echo "Name: $name <br>";

echo "Email: $email <br>";

echo "Gender: $gender <br>";

if (isset($_POST['agree'])) {

echo "Terms and Conditions: Accepted";

} else {

echo "Terms and Conditions: Not Accepted";

Gramin Technical & managemnet campus 3


WBP CO6I Practical 10

?>

</body>

</html>

OUTPUT:

Q2. Write a program to design a registration form using textbox,


radio button, checkbox and button.
CODE:
<!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>

Gramin Technical & managemnet campus 4


WBP CO6I Practical 10

<style>

.error { color: red; }

</style>

</head>

<body>

<h2>Registration Form</h2>

<form method="post" action="process_registration.php">

<label for="fullname">Full Name:</label>

<input type="text" id="fullname" name="fullname" required>

<br><br>

<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>

<input type="radio" id="other" name="gender" value="other">

<label for="other">Other</label>

<br><br>

<label for="email">Email:</label>

<input type="email" id="email" name="email" required>

<br><br>

<label>Languages Known:</label>

<input type="checkbox" id="english" name="languages[]" value="english">

<label for="english">English</label>

<input type="checkbox" id="spanish" name="languages[]" value="spanish">

<label for="spanish">Spanish</label>

Gramin Technical & managemnet campus 5


WBP CO6I Practical 10

<input type="checkbox" id="french" name="languages[]" value="french">

<label for="french">French</label>

<br><br>

<input type="submit" value="Register">

</form>

</body>

</html>

OUTPUT:

Gramin Technical & managemnet campus 6

You might also like