PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Form Validation using PHP

Form Validation using PHP


Today I'll take it up a level and re-write that script using the if, else conditional and empty() function to include form validation.

Using an if, else conditional: if (condition) {do something} else { do this} , we can make certain fields of our form required that they be filled out. Let's start by making a basic form with first name, last name, email and comments as our text inputs. Copy the following code into a text document then save and name it form.html:





#form {
width: 500px;
margin-right: auto;
margin-left: auto;
}
->








Enter your information in the form below.

First Name:




Last Name:




E-Mail:




Comments:











The action for the form is the same as the last form tutorial (send.php) but we'll re-write it now to include the changes. The easiest way to check to see if a user has typed in a value in the text boxes is to use the empty() function. Using the old send.php the code looked like this for the Name variable: $msg = "Name; $_POST[name] "; . Using the empty() function and an if, else conditional to make sure the user typed in a value for the "first name" text box, the revised script is written like this:

if (!empty($_POST['fname'])){
$msg = "fname; $_POST[fname] ";
}else{
$fname = NULL;
echo "Please fill out your first name.
";
}

In the above script the empty() function checks to see if the "first name" box has a value. If yes, the value of the $fname variable would be sent to the recipients email. If the box is empty the $fname variable is set to NULL and the user sees the message "please fill out your first name" followed by a line break
. The " " that you see in the string simply adds a line break to the information being sent so that when the recipient receives the email, its not one continuous line.

So, the entire PHP script to check all text inputs is:


if (!empty($_POST['fname'])){
$msg = "fname; $_POST[fname] ";
}else{
$fname = NULL;
echo "Please fill out your first name.
";
}
if (!empty($_POST['lname'])){
$msg .= "lname: $_POST[lname] ";
}else{
$lname = NULL;
echo "Please fill out your last name.
";
}
if (!empty($_POST['email'])){
$msg .= "email: $_POST[email] ";
}else{
$email = NULL;
echo "Please leave your email address.
";
}
if (!empty($_POST['comments'])){
$msg .= "comments: $_POST[comments] ";
}else{
$comments = NULL;
echo "You forgot to leave a comment.
";
}
$recipient = "yourname@yourdomain.com";
$subject = "Form Feedback";
$mailheaders = "Reply-to: $_POST[email]";

//send the mail

mail($recipient, $subject, $msg, $mailheaders);

?>

Put this between the body tags of a new html document (make sure to change the $recipient to your email address) then save and name it send.php . Style the form.html to match your website then upload both the form.html and send.php to your server in the same directory you keep all of your other web site pages, and your done.



Author's URL: 2torial Blog
PHP is open source scripting language. It\'s widely used to develop web applications. More PHP Tutorials: Featured Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Reader's comments
comments adroja October 13, 2011 says:
greate but i can't use this becouse it is not that which i want
Reply
comments Cheap Nike Air Max September 24, 2011 says:
Welcome to our good reputation online store. Here [url= Nike Shox R4 White Pink Shoes[/url] is designer shoes,cheap nike air max,cheap nike shox mens and nike shox clearance market. Our company is a professional manufacturer and exporter,dealing brand shoes like nike dunks,[url= Nike Shox R4 White Gray Purple Shoes[/url] Nike air max skyline,cheap mens nike air max 2012,new nike air max 2012 mens sale,cheap nike shox R4 mens,nike shox R4 mens sale,[url= Nike Shox R4 White Gray Purple Shoes[/url] mens nike shox R4 sale,nike shox R4 sale,cheap nike shox R4 and so on,nike air max 90,180,360,2011
Reply
comments calvin klein underwear September 24, 2011 says:
Our good reputation online store offers a number of popular products</strong> <strong><a href=" Klein Underwear X Global Trunk</a></strong>. All products is wholesale price. We offered many brand of <strong><a href=" Klein Underwear X Modal Boxers</a></strong>, mens calvin klein underwear, calvin klein underwear, oakley sunglasses on sale, <a href=" Klein Underwear Mens Boxers Cotton</a>,adidas shoes, red bull caps, belts, our web would be the best choice. Everyone will love them. fast delivery,discount price and 1
Reply
comments Ricardo Martins January 10, 2011 says:
great article...

Thanks you

Reply
comments quyishopquyishop December 22, 2010 says:
[url= outlet[/url] can't be replaced by anything else. [url= air max sale[/url] It's certain that once i lose it, i won't live as a happy woman. Maybe you think i'm crazy, but that is the truth. I'm just so keen on gucci.
Reply
comments hardik November 24, 2010 says:
this is nice
Reply
comments amol October 22, 2010 says:
its really helpful dude
Reply
comments Crambook September 20, 2010 says:
Well this is good for newbie, but i think this code can not be use in a website that needs to display dynamically and stylishly. When the use doesnt enter any entry and he clicked "Submit information" button, the sendmail.php will only generally error message in another page, which will make it difficult for some users to navigate through the website. Sorry for my English, i hope you understand me.
Reply
comments Martin Gill June 08, 2011 says:
lkklkl
Reply
comments rahul August 02, 2010 says:
thank u its good bt can u tel me clearly where to make changes(my e-mail)is it any email i wish to write ?
Reply
comments nishit August 02, 2010 says:
i got , but not to understand
Reply
comments Datta July 06, 2010 says:
yes is a write code thank you for this code!
Reply
comments telecommute php mysql coder June 23, 2010 says:
This is a nice script, And easy to learn something about it.
And using this code is very simple.

Thanks a lot..!

Reply
comments suresh May 18, 2010 says:
Thank you its very usseful built mysite
Reply
comments Nitin Surani April 30, 2010 says:
This php validation query is very good and excellent. and vary esay to put.
i like this script.

Reply
comments ss April 15, 2010 says:
12345
Reply
Add comments to "Form Validation using PHP"

Captcha