PHP Validation form - learnit

Home Top Ad

Post Top Ad

Saturday, June 6, 2020

PHP Validation form

C Programming How to Use switch Statement

What is PHP

PHP is a server side scripting language. that is used to develop Static websites or Dynamic websites or Web applications. PHP stands for Hypertext Pre-processor, that earlier stood for Personal Home Pages.


PHP scripts can only be interpreted on a server that has PHP installed.


The client computers accessing the PHP scripts require a web browser only.


Why use PHP?

You have obviously heard of a number of programming languages out there; you may be wondering why we would want to use PHP as our poison for the web programming. Below are some of the compelling reasons.


  - PHP is open source and free.

  - Short learning curve compared to other languages such as JSP, ASP etc.

  - Large community document

  - Most web hosting servers support PHP by default unlike other languages such as ASP that need IIS. This makes PHP a cost effective choice.

  - PHP is regular updated to keep abreast with the latest technology trends.

  - Other benefit that you get with PHP is that it’s a server side scripting language; this means you only need to install it on the server and client computers requesting for resources from the server do not need to have PHP installed; only a web browser would be enough.

  - PHP has in built support for working hand in hand with MySQL; this doesn’t mean you can’t use PHP with other database management systems. You can still use PHP with

    +Postgres

    +Oracle

    +Oracle

    +ODBC etc.

  - PHP is cross platform; this means you can deploy your application on a number of different operating systems such as windows, Linux, Mac OS etc.


Form Validation in PHP

An HTML form contains various input fields such as text box, checkbox, and radio buttons, submit button, and checklist, etc. These input fields need to be validated, which ensures that the user has entered information in all the required fields and validates that the information provided by the user is valid and correct.There is no guarantee that the information provided by the user is always correct. PHP validates the data at the server-side, which is submitted by HTML form. You need to validate a few things:

Name Description
Empty String The code below checks that the field is not empty. If the user leaves the required field empty
Validate String The code below checks that the field will contain only alphabets and whitespace, if the Book field does not receive valid input from the user.
Validate Number The below code validates that the field will only contain a numeric value. Example MobileNumber. If The Mobile_Number no field does not receive Data from the User.
Validate Email A valid email must contain @ and. symbols. PHP provides various methods to validate the email address. The below code validates the email address provided by the user through HTML form. If the field does not contain a valid email address
Length Validate The input length validation restricts the user to provide the value between the specified range

Empty String

The code below checks that the field is not empty. If the user leaves the required field empty, it will show an error message.

Example


If (empty(&_Post["name"])){
    &errMessage ="Error Input Your Name"; echo &errMessage;
}else { &Name=$_POST["name"];


Validate String

The code below checks that the field will contain only alphabets and whitespace, if the Book field does not receive valid input from the user.


Example

&name=&_Post["BookName"];

If(!preng_match("/^[a-zA-z]*$/", $name) ) {

&ErrMessage;

else {

echo &name;

}


Validate Number

The below code validates that the field will only contain a numeric value. Example MobileNumber. If The Mobile_Number no field does not receive Data from the User.


Example

$mobileNumber = $_POST ["Mobile_Number"];

if (!preg_match ("/^[0-9]*$/", .&mobileNumber) ){

.&ErrMessage= "Only numeric value is allowed.";

echo &ErrMessage;

} else {

echo &mobileNumber;

}


Validate Email

A valid email must contain @ and. symbols. PHP provides various methods to validate the email address. The below code validates the email address provided by the user through HTML form. If the field does not contain a valid email address


Example

$Myemail = &_POST ["Email" ];

$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})^$";

if(!preg_match ($pattern, $Myemail) ){

$ErrMessage = "Your Email is not valid.";

echo $ErrMessage;

else{

echo "Your valid email address is:".$MyEmail;

}


Validate URL

The below code validates the URL of website provided by the user via HTML form. If the field does not contain a valid URL. The Show Will display on Error Message.


Example

$MywebsiteURL = $_POST["Mywebsite"];

if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$Mywebsite)) {

$websiteErrMessage = "URL is not valid";

echo $websiteErrMessage;

}else {

echo "Website URL is: " .$MywebsiteURL;

}


Input Length Validate

The input length validation restricts the user to provide the value between the specified range


Example

$LengthID = strlen ($_POST ["LengthID"]);

$length = strlen ($LengthID);

if (&length < 20 && &length > 20) {

$ErrMessage = "ID must have 20 digits.";

echo $rreMessage

echo $erreMssage;

}else{

echo "Your Mobile number is: " .$LengthID;

}

Example

<html>
<head>
<title> PHP </title>
</head>
<body>
<?php
$name= $email= $phone= $gender="";
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$name=test($_POST["name"]);
$email=test($_POST["email"]);
$phone=test($_POST["phone"]);
$gender=test($_POST["gender"]);
}
function test($data)
{
$data=htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<h2> Knowledge Education Cambodia <u> (Validation) </u> </h2>
Name: <input type="text" name="name">
<br/>
Email: <input type="text" name="email">
<br/>
Phone: <input type="text" name="phone">
<br/>
Gender:
<select name="gender">
<option> Male </option>
<option> Female </option>
</select>
<br/>
<input type="submit" value="Add" name="submit">
<?php
echo "<h2> Your Output </h2>";
echo "<br/>";
echo "Name" ; echo $name;
echo "<br/>";
echo "email" ; echo $email;
echo "<br/>";
echo "Gender" ; echo $gender;
echo "<br/>";
echo "Phone" ; echo $phone; echo "<br/>";
?>
</body>
</html>

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad