|
In this article I will show you how to create and validate a
form using HTML and PHP. First of all in PHP form field validation check the
form will be created in HTML 5 and then validated and processed the contents of
the form using PHP.
The form
will be having a name field, address field, an email field and a submit button.
The contents of these form fields will be validated in order to check whether
the user has entered valid data into the fields or not. We will also check if
any of the fields is left blank and we will display an appropriate message to
let the user know that there is an error in the form fields. If you want to know how to use JavaScript Form Validation you can check Form validation using JavaScript.
HTML FORM
First of all in PHP form field validation check, let us take
a quick look at the form design. The form will be used to capture details like
name, address and email of the users.
<form align="center"action="" method="POST">
<p><label for="name">Name:</label></p>
<p><input type="text" name="name"></p>
<p><label for="address">Address:</label></p>
<p><input type="text" name="address"></p>
<p><label for="email">Email:</label></p>
<p><input type="email" name="email"></p>
<input type="submit" name="signup" value="Sign Up!"><br>
</form>
FORM PROCESSING
Now that our form for PHP form field validation check is
ready and the next stage is to process the form using PHP. The form will be
sent to the web server when the user will click on the submit button subject
that there are no errors on the form otherwise there will be errors displayed
on your HTML form.
The complete source code of this example of PHP form field validation check is as under:
<?php
$con=mysqli_connect("localhost","root","","youtube");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP form field validation check</title>
<style>
.wrapper{
width:500px;
margin:50px auto;
}
body{
background-color: #e33;
font-size: 18px;
font-family: verdana,sans-serif;
color: #fff;
}
input[type="text"],input[type="email"]{
width:300px;
height:30px;
}
input[type="submit"]{
width:300px;
height:30px;
margin-bottom: 20px;
}
form{
border:1px solid#999;
box-shadow: 1px 1px 1px 1px #fff;
padding:10px;
}
</style>
</head>
<body>
<div class="wrapper">
<form align="center"action="" method="POST">
<p><label for="name">Name:</label></p>
<p><input type="text" name="name"></p>
<p><label for="address">Address:</label></p>
<p><input type="text" name="address"></p>
<p><label for="email">Email:</label></p>
<p><input type="email" name="email"></p>
<input type="submit" name="signup" value="Sign Up!"><br>
<?php
if(isset($_POST['signup'])){
$name=$_POST['name'];
$address=$_POST['address'];
$email=$_POST['email'];
if($name==""){
echo"Name can not be left blank";
echo"<br>";
}
elseif($address==""){
echo"Address can not be left blank";
echo"<br>";
}
elseif($email==""){
echo"Email can not be left blank";
echo"<br>";
}
else{
$query="insert into students (st_name,address,email) values ('$name','$address','$email')";
$row=mysqli_query($con,$query);
if($row){
echo"Record inserted successfully";
}
}
}
?>
</form>
</div>
</body>
</html>
The video of this article is available on YouTube and you can watch it here.
0 comments:
Post a Comment