0% found this document useful (0 votes)
59 views

!doctype HTML Head Style .Error /style /head Body

This code is validating user input from an HTML form submission in PHP. It defines variables to store each form field value and associated error messages. On form submission, it checks if fields are empty and validates the values, like checking email format. Any errors found are stored in error variables. A test_input() function strips slashes and special characters from inputs. The form is redisplayed with the submitted values and any error messages.

Uploaded by

IFRA JAWED
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

!doctype HTML Head Style .Error /style /head Body

This code is validating user input from an HTML form submission in PHP. It defines variables to store each form field value and associated error messages. On form submission, it checks if fields are empty and validates the values, like checking email format. Any errors found are stored in error variables. A test_input() function strips slashes and special characters from inputs. The form is redisplayed with the submitted values and any error messages.

Uploaded by

IFRA JAWED
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment # 1 WDD-13/9/19

Run following code, explain what you understand. Explain each function variable.

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
// $_SERVER['REQUEST_METHOD'] returns the request method used to access the page (such
as POST)
if ($_SERVER["REQUEST_METHOD"] == "POST") {
/* PHP $_POST is widely used to collect form data after submitting an HTML form with
method="post". */
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
// test_input() tests the value of input then save and post the value
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
// preg_match – this function is used to perform a pattern match on a string.
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space allowed";
}
}
// if input field is empty then print email is required.
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);

// check if e-mail address is well-formed


// The FILTER_VALIDATE_EMAIL filter validates value as an e-mail address.
// The filter_var() function filters a single variable with a specified filter.
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
Assignment # 1 WDD-13/9/19

if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);

// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-
9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "Invalid URL";
}
}

if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
// if input field is empty then print gender is required.
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
}
// test_input() function tests the value of input then return the value
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>

<h2>PHP Form Validation Example</h2>


<p><span class="error">* required field</span></p>

// method =”post” is use to collect save the form data after submitting HTML form into PHP.
// The htmlspecialchars() function converts some predefined characters to HTML entities.
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $name;?>">
// php echo print the value of $nameErr after submitting an empty field, it shows error.
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Assignment # 1 WDD-13/9/19

Website: <input type="text" name="website" value="<?php echo $website;?>">


<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"><?php echo
$comment;?></textarea>
<br><br>
Gender:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo
"checked";?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo
"checked";?> value="male">Male
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="other") echo
"checked";?> value="other">Other
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
// after submitting an html form the data is collected and saved in php
<?php
// echo prints the data of submitting HTML form through php.
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

</body>
</html>

You might also like