PHP 7 Form

A form contains numerous fields, or space to enter inputs from users. It is used to insert, update, delete data in a database.

Example -

This is the simple form having two text type input fields, one email type, one checkbox to check term & condition, radio buttons to check marital status, and for password and confirm password.

When you submit the form, then the fields values are send to myformdata.php file using POST HTTP method.


<form method="post" name="myform" action="myformdata.php">
	Name: <input type="text" name="name" value="">
	Age: <input type="text" name="age" value="">
	EmailId: <input type="email" name="email" value="">     
	Marital Status: 
	Single <input type="radio" name="status" value="single"/>
	Married <input type="radio" name="status" value="married"/>
	Password: <input type="password" name="password" value="">
	Comfirm password: <input type="password" name="confirm" value="" >
	Term & condition 
	<input type="checkbox" value="" name="termcheck">
	I understand that information we enter will be viewable by other users of the system. 
	<input type="submit" value="Submit" name="Submit">                
</form>   

In myformdata.php file, we can get data using $_POST superglobal array.

myformdata.php
Name: <?php echo $_POST['name']; ?>
Age: <?php echo $_POST['age']; ?>
Emailid: <?php echo $_POST['email']; ?>
Marital Status: <?php echo $_POST['status']; ?>




PHP Form Validation

Validation is used to check whether the form data entered correctly or not. By using PHP, we can apply server side form validation. Here is a simple example to check all the entered input data in the post.

<?php
if(isset($_POST['submit'])){
 if(empty($_POST['name'])) {
	$nameerror = "Name is required";
 }
 if(empty($_POST['age'])) {
	$ageerror = "Age is required";
 }
 if(empty($_POST['email'])) {
	$emailerror = "Email is required";
 }
 if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$_POST['email'])) {
	$emailerror = "Invalid Email Format";
 }
 if(empty($_POST['status'])) {
	$statuserror = "Please Check Marital Status";
 }
 if(empty($_POST['password'])) {
	$pwderror = "Please enter password";
 }
 if(empty($_POST['confirm'])) {
	$confirmerror = "Please Confirm password";
 }
 if($_POST['password'] != $_POST['confirm']) {
	$bothpwderror = "Both Password and Confirm password are not match";
 }
 if($_POST['termcheck'] == 'false') {
	$termerror = "Please Check term & condition";
 }        
}
?>

<form method="post" name="myform" action="/index.php" >
	Name : <input type="text" name="name" value="">
	<span class="error">* <?php echo $nameerror;?>
	Age : <input type="text" name="age" value="">
	<span class="error">* <?php echo $ageerror;?>
	EmailId : <input type="email" name="email" value="">
	<span class="error">* <?php echo $emailerror;?>    
	Term & condition <span><input type="checkbox" value=""  name="termcheck">
	I understand that information we enter will be viewable by other users of the system.  
	<span class="error">* <?php echo $termerror;?>
	Marital Status: 
	Single <input type="radio" name="status" value="single"/>
	Married  <input type="radio" name="status" value="married"/>
	<span class="error">* <?php echo $statuserror;?>
	Password : <input type="password" name="password" value="">
	<span class="error">* <?php echo $pwderror; ?>
	Comfirm password: <input type="password" name="confirm" value="" >
	<span class="error">* <?php echo $confirmerror;?>
	<span class="error">* <?php echo $bothpwderror;?>
	<input type="submit" value="Submit" name="Submit">               
</form>

Entered Data - Name: <?php echo $_POST['name']; ?> Age: <?php echo $_POST['age']; ?> Emailid: <?php echo $_POST['email']; ?> Marital Status: <?php echo $_POST['status']; ?>





Read more articles


General Knowledge



Learn Popular Language