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

WP - Form Validation

Php form validation

Uploaded by

Yuvak Guru
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)
6 views

WP - Form Validation

Php form validation

Uploaded by

Yuvak Guru
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
You are on page 1/ 4

<html>

<head>

<style>

.error {

Color: red;

</style>

</head>

<body>

<?php

$nameErr = $emailErr = $genderErr = $websiteErr = “”;

$name = $email = $gender = $comment = $website = “”;

$ok = 1;

If ($_SERVER[“REQUEST_METHOD”] == “POST”) {

If (empty($_POST[“name”])) {

$nameErr = “Name is required”;

$ok = 0;

} else {

$name = test_input($_POST[“name”]);

If (!preg_match(“/^[a-zA-Z- ]+$/”,$name)) {

$nameErr = “Only letters and white space allowed”;

$ok = 0;

}
If (empty($_POST[“email”])) {

$emailErr = “Email is required”;

$ok = 0;

} else {

$email = test_input($_POST[“email”]);

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

$emailErr = “Invalid email format”;

$ok = 0;

If (empty($_POST[“website”])) {

$website = “”;

} else {

$website = test_input($_POST[“website”]);

If (!filter_var($website, FILTER_VALIDATE_URL)) {

$websiteErr = “Invalid URL”;

$ok = 0;

If (empty($_POST[“comment”])) {

$comment = “”;

} else {

$comment = test_input($_POST[“comment”]);

If (empty($_POST[“gender”])) {
$genderErr = “Gender is required”;

$ok = 0;

} else {

$gender = test_input($_POST[“gender”]);

Function test_input($data) {

$data = trim($data);

$data = htmlspecialchars($data);

Return $data;

?>

<h2>PHP Form Validation Example</h2>

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

<form method=”post” action=””>

Name: <input type=”text” name=”name” value=”<?php echo $name;?>”>

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

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” 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=”submit” name=”submit” value=”Submit”>

</form>

<?php

If(isset($_POST[‘submit’]) && $ok == 1)

Echo “<h2>Your Input:</h2>”;

Echo $name . “<br>”;

Echo $email . “<br>”;

Echo $website . “<br>”;

Echo $comment . “<br>”;

Echo $gender;

?>

</body>

</html>

You might also like