Form Validate Data With PHP
The most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element will automatically be available to your PHP scripts.
The most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element will automatically be available to your PHP scripts.
<form action="validate.php" method="post"> <p>Your name: <input type="text" name="txtname" /></p> <p>Your age: <input type="text" name="txtage" /></p> <p><input type="submit" /></p> </form>
When the user fills in this form and hits the submit button, the validate.php page is called. In this file you would write something like this:
Welcome <?php echo htmlspecialchars($_POST['txtname']); ?>. You are <?php echo (int)$_POST['txtage']; ?> years old.
Output:
Welcome Jai. You are 25 years old.
Various input fields: required and optional text fields, radio buttons, and a submit button:
Field Name | Validation Rules |
---|---|
Name | Required. + Must only contain letters and whitespace |
Required. + Must contain a valid email address (with @ and .) | |
Website | Optional. If present, it must contain a valid URL |
Comment | Optional. Multi-line input field (textarea) |
Gender | Required. Must select one |
When use the htmlspecialchars() function; then if a user tries to submit the following in a text field:
We will also do two more things when the user submits the form:
- Strip unnecessary characters (extra space, tab, newline) from the user input data (with the PHP trim() function)
- Remove backslashes (\) from the user input data (with the PHP stripslashes() function).
We check whether form has been submitted using $_SERVER["REQUEST_METHOD"]. If the REQUEST_METHOD is POST, then the form has been submitted - and it should be validated. If it has not been submitted, skip the validation and display a blank form.
Example 1 :
<?php
// define variables and set to empty values$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"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<form method="post" action="<?php echohtmlspecialchars($_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
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
Example 2 :
<form method="POST" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]);?>
htmlspecialchars($_SERVER["PHP_SELF"]);?>
">
Name <input name="name" type="text" value="" />
Address <input name="address" type="text" value="" />
Email <input name="email" type="text" value="" />
<input name="howMany" type="radio" value="zero" /> 0
<input name="howMany" type="radio" value="one" /> 1
<input name="howMany" type="radio" value="two" /> 2
<input name="howMany" type="radio" value="twoplus" /> More than 2
<select multiple="" name="favFruit[]" size="4">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="plum">Plum</option>
<option value="pomegranate">Pomegranate</option>
<option value="strawberry">Strawberry</option>
<option value="watermelon">Watermelon</option>
</select>
<input name="brochure" type="checkbox" value="Yes" />
<input name="submit" type="submit" value="Submit" />
</form>
<?php
// define variables and initialize with empty values
$nameErr = $addrErr = $emailErr = $howManyErr = $favFruitErr = "";
$name = $address = $email = $howMany = "";
$favFruit = array();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Missing";
}
else {
$name = $_POST["name"];
}
if (empty($_POST["address"])) {
$addrErr = "Missing";
}
else {
$address = $_POST["address"];
}
if (empty($_POST["email"])) {
$emailErr = "Missing";
}
else {
$email = $_POST["email"];
}
if (!isset($_POST["howMany"])) {
$howManyErr = "You must select 1 option";
}
else {
$howMany = $_POST["howMany"];
}
if (empty($_POST["favFruit"])) {
$favFruitErr = "You must select 1 or more";
}
else {
$favFruit = $_POST["favFruit"];
}
}
// the HTML code starts here
No comments:
Post a Comment