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

3.PHP Form Validation

PHP provides various methods and arrays for form validation and processing form data, including isset() to check if a variable is set, $_GET and $_POST to retrieve form data, and $_SERVER to access server variables. Forms can be validated by checking for empty fields, valid email addresses, and other patterns. Errors can be displayed next to invalid fields using conditional styling.

Uploaded by

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

3.PHP Form Validation

PHP provides various methods and arrays for form validation and processing form data, including isset() to check if a variable is set, $_GET and $_POST to retrieve form data, and $_SERVER to access server variables. Forms can be validated by checking for empty fields, valid email addresses, and other patterns. Errors can be displayed next to invalid fields using conditional styling.

Uploaded by

kashish7377
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

PHP

FORM VALIDATION
INPUT TAG-ATTRIBUTES
PHP METHODS AND ARRAYS
isset() determine whether the variable or a form control is having a value
or not
$_GET[] • retrieve the information from the form control through the
parameters sent in the URL
• It takes the attribute given in the url as the parameter
$_POST[] • retrieve the information from the form control through the
HTTP POST method
• takes name attribute of corresponding form control as the
parameter
$_REQUEST[] used to retrieve an information while using a database
$_SERVER[] holds information about headers, paths, and script
locations.
Both GET and POST create an array (e.g. array( key1 => value1, key2 => value2, key3 =>
value3, ...))
PHP METHODS
GET POST
Information sent from a form with the invisible to others (all names/values
GET method is visible to are embedded within the body of the
everyone (all variable names and HTTP request)
values are displayed in the URL)
limits on the amount of information to No limit on the amount of information
send(2000 characters) to send

Prefer POST for sending form data.


PHP FORM
<!DOCTYPE HTML>
<html>
<body>

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


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
PHP FORM
welcome.php

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>


Your email address is: <?php echo $_POST["email"];
?>

</body>
</html>
PHP FORM
PHP FORM
<html>
<head>
<title>Form</title>
</head>
<body>
<h1>Registration form.</h1>
<form method = "post" action = "form2.php">
<br />
<label>First Name</label>
<input type = "text" name = "fname" /><br />
<label>Last Name</label>
<input type = "text" name = "lname" /><br />
<label>Email</label>
<input type = "text" name = "email" /><br />
<label>Phone</label>
<input type = "text" name = "phone" /><br />
PHP FORM
<span style = "color: blue">
Which book would you like information about? </span><br/>
<select name = "book">
<option>Internet and WWW How to Program 2e</option>
<option>C++ How to Program 3e</option>
<option>Java How to Program 4e</option>
</select>
<br /><br /><br /><span style = "color: blue">
Which operating system are you currently using?
<br /></span>
<input type = "radio" name = "os" value = "Windows NT" checked = "checked”/> Windows NT
<input type = "radio" name = "os" value = "Windows 2000" /> Windows 2000
<input type = "radio" name = "os" value = "Windows 98" /> Windows 98<br />
<input type = "radio" name = "os" value = "Linux" /> Linux
<input type = "radio" name = "os" value = "Other" /> Other<br />
<br />
<input type = "submit" value = "Register" /></form></body>
PHP FORM
<head>
<title>Form Validation</title>
</head>
<body style = "font-family: arial,sans-serif">
<p>Hi
<span style = "color: blue">
<strong>
<?php echo $_POST["fname"]; ?>
</strong>
</span>.
Thank you for completing the survey.<br />
You have been added to the
<span style = "color: blue">
<strong>
<?php echo $_POST["book"]; ?>
</strong>
</span>
mailing list.
</p>
PHP FORM
<strong>The following information has been saved in our database:</strong><br/>
<table border = "2" cellpadding = "0" cellspacing = "10">
<tr>
<td bgcolor = "#ffffaa">Name </td>
<td bgcolor = "#ffffbb">Email</td>
<td bgcolor = "#ffffcc">Phone</td>
<td bgcolor = "#ffffdd">OS</td>
</tr>

<tr>
<?php
$a=$_POST["fname"];
$b=$_POST["email"];
$c=$_POST["phone"];
$d=$_POST["os"];
PHP FORM VALIDATION

// print each form field’s value


echo "<td>$a</td>
<td>$b</td>
<td>$c</td>
<td>$d</td>";
?>
</tr>
</table>

</body>
PHP FORM
PHP FORM
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<?php

$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
PHP FORM

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
PHP FORM
<h2>PHP Form Validation Example</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
">
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
Website: <input type="text" name="website">
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></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
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
PHP FORM VALIDATION
PHP FORM VALIDATION
<<!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 = "";
PHP FORM VALIDATION
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);

if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space
allowed";
}
}
PHP FORM VALIDATION
if (empty($_POST["email"])) {
$emailErr = "Email is required";
}
else {
$email = test_input($_POST["email"]);
if(!filter_var($email,FILTER_VALIDATE_EMAIL))
{
$emailErr = "Invalid email format";
}
}
PHP FORM VALIDATION

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

if (!preg_match("/^([a-z\d](-*[a-z\d])*)(\.([a-
z\d](-*[a-z\d])*))*$/i",$website)) {
$websiteErr = "Invalid URL";
}
}
PHP FORM VALIDATION

if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment =
test_input($_POST["comment"]);
}
PHP FORM VALIDATION

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

function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
PHP FORM VALIDATION
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVE
R["PHP_SELF"]);?>">
Name: <input type="text" name="name" value="<?php echo $name;
?>">
<span class="error">* <?php echo $nameErr;?></span> <br><br>
Email: <input type="text" name="email" value="<?php echo $ema
il;?>">
<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 ec
ho $comment;?></textarea> <br><br>
PHP FORM VALIDATION

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>

You might also like