Simple form with validation code using php

Simple-form-with-validation-code-using-php

Simple form with validation code using php 

Step 1 : Create a file and paste the below code :-

<form method="POST" action="">
<input type="text" class=""  name="name">
<input type="email" class="" name="email">
<input type="submit" name="submit">
</form>

 

Firstly Create a form in HTML and write METHOD = "post" like :-

method="POST"

 

Declare Variables for print Errors :- 

$error = "" ;
$name_error = "" ;
$email_error = "" ;

 

Then the Validation Code of PHP 

<?php
$error = "" ;
$name_error = "" ;
$email_error = "" ;

if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['submit']) )
{
if (empty($_POST['name']) && empty($_POST['email'])) 
{
    $error = "Name and Email Required";
}

elseif (empty($_POST['name'])  && !empty($_POST['email']))
{

	$name_error = "Name is Required";
}


elseif (!empty($_POST['name'])  && empty($_POST['email']))
{

	$email_error = "Email is Required";
}
else {
	echo "okk dara inserted";
}



}

else 
{
	echo "Fill Form ";
}
?>

 

$_SERVER['REQUEST_METHOD'] == "POST" 

The $_SERVER is used for take input from post/form/input field

 

Create a style 

<style type="text/css">
	.red-outline
	{
		border: 1px solid red;
	}
</style>

 

Full code for form validation

<?php
$error = "" ;
$name_error = "" ;
$email_error = "" ;

if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST['submit']) )
{
if (empty($_POST['name']) && empty($_POST['email'])) 
{
    $error = "Name and Email Required";
}

elseif (empty($_POST['name'])  && !empty($_POST['email']))
{

	$name_error = "Name is Required";
}


elseif (!empty($_POST['name'])  && empty($_POST['email']))
{

	$email_error = "Email is Required";
}
else {
	echo "okk dara inserted";
}



}

else 
{
	echo "Fill Form ";
}
?>

<style type="text/css">
	.red-outline
	{
		border: 1px solid red;
	}
</style>

<form method="POST" action="">

	<?php echo $error; ?>
	<br>
	<?php echo $name_error; ?>
	
	
	<input type="text" class="
	<?php
if(isset($_POST['submit']) )
{
if (empty($_POST['name'])) 
{

	echo "red-outline";
}

else {
	echo "null-class";
}
}

else 
{
	echo "no data report";
}
?>
    "  name="name">
    <br>
    <?php echo $email_error ; ?>
    <br>
	<input type="email" class="
	<?php
if(isset($_POST['submit']) )
{
if (empty($_POST['email'])) 
{

	echo "red-outline";
}

else {
	echo "null-class";
}
}

else 
{
	echo "Form Fill Successfully";
}
?>
    " name="email">
    <br>
    <br>
    <input type="submit" name="submit">
</form>

 

ADD COMMENT

Your email address will not be published. Required fields are marked *








Be the first to comment. :)

The primary motive of our company is to provide beyond satisfactory results .

Request a free consultation quote

WWW.BHAGAUTI.COM © COPYRIGHT 2018 | TERMS AND CONDITIONS | Privacy & Policy