Chapter 2 - PHP Forms
Chapter 2 - PHP Forms
PHP Forms
Sending form data
⚫ An HTML form on a web page is nothing more than a
$userName = $_POST["userName"];
if (!preg_match("/^[a-zA-Z0-9_]*$/", $userName))
{
$errorMsg = "Only alphabets, numbers, and underscores are
allowed for User Name";
}
Else
{
echo $userName;
}
Number Validation
• Number validation in an HTML form ensures that users enter valid
numeric input in a form field.
• HTML provides built-in attributes and methods to validate numbers
without
Attributerequiring JavaScript.
Description Example
min Minimum value allowed. <input type="number" min="1">
max Maximum value allowed. <input type="number" max="100">
Defines the interval between
step <input type="number" step="5">
valid values.
Ensures the field is
required <input type="number" required>
mandatory.
value Sets a default number. <input type="number" value="10">
Defines a custom regex
<input type="text" pattern="[0-9]+"
pattern pattern (for <input
required>
type="text">).
Number Validation
⚫ The following example checks whether the user has
added only numbers in the required input field.
$age = $_POST["age"];
if (!preg_match("/^[0-9]*$/", $age))
{
$errorMsg = "Only numeric values are allowed!!";
}
Else
{
echo $age;
}
Email Validation
⚫ This is used to check if user has added a valid email in the
required input field.
$emailID = $_POST["emailID"];
$pattern =
"^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})
$^";
if (!preg_match($pattern, $emailID) )
{
$errorMsg = "Invalid Email ID format";
}
Else
{
echo $emailID;
}
Count Length
⚫ This is used to check if the length of the string is in valid range.
Here, strlen() is used to find the length of the string.
$phoneNo = $_POST["phoneNo"];
if (strlen($phoneNo) != 10)
{
$errorMsg = "Please provide a phone number of 10 digits!!";
}
Else
{
echo $phoneNo;
}
URL Validation
⚫ This is used to check if user has added a valid URL in the required input
field.
$url = $_POST["url"];
$pattern =
"/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-
9+&@#\/%=~_|]/i"
if (!preg_match($pattern, $url))
{
$errorMsg = "You entered an INVALID URL";
}
else
{
echo $url;
}
Sending Values to a Script Manually
⚫ There are two different ways you can pass variables and
values to a PHP script, both worth knowing.
⚫ The first is to make use of HTML's hidden input type:
www.dmcinsights.com/page.php?name=Brian
PHP Regular Expressions
Function Description
preg_match() Returns 1 if the pattern was found in the string
and 0 if not
preg_match_all() Returns the number of times the pattern was
found in the string, which may also be 0
Using preg_replace()
Expression Description
[abc] Find one or many of the characters inside the
brackets
[^abc] Find any character NOT between the brackets
[a-z] Find any character alphabetically between two
letters
[A-z] Find any character alphabetically between a
specified upper-case letter and a specified
lower-case letter
[A-Z] Find any character alphabetically between two
upper-case letters.
[123] Find one or many of the digits inside the brackets
Metacharacters
Metacharacter Description
| Find a match for any one of the patterns separated by | as
in: cat|dog|fish
. Find any character
^ Finds a match as the beginning of a string as in: ^Hello
$ Finds a match at the end of the string as in: World$
\d Find any digits
\D Find any non-digits
\s Find any whitespace character
\S Find any non-whitespace character
\w Find any alphabetical letter (a to Z) and digit (0 to 9)
\W Find any non-alphabetical and non-digit character
\b Find a match at the beginning of a word like this: \bWORD,
or at the end of a word like this: WORD\b
\uxxxx Find the Unicode character specified by the hexadecimal
number xxxx
Quantifiers
Quantifier Description
n+ Matches any string that contains at least one n