How To Get Form Data In PHP Using $_POST Method?

php Jun 24, 2019

So before understanding how to get data in php Through form , lets see

What is form?

Forms are utilized to get a contribution from the client and submit it to the web server for handling.

A form is a HTML <tag> that contains graphical UI things, for example, input box, check boxes radio buttons and so forth.

The form is characterized utilizing the labels and GUI things are characterized utilizing structure components, for example, input.

When to use forms?

1) Forms prove to be useful when creating adaptable and dynamic applications that acknowledge client input.

2) Forms can be utilized to alter effectively existing information from the database.

So lets get started ,

Lets make a form using bootstrap CSS ,

<html>
<head>
<title>My Form</title>
</head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<body>

<div class="container">
  <form action="verify.php" method="POST">
  <div class="card p-5">
     <div class="row">
    	<div class="col-lg-6 col-md-6 col-sm-12">
      		<div class="form-group">
        		<label for="">Name</label>
        		<input type="text" name="name" class="form-control">
      		</div>
    	</div>
    	<div class="col-lg-6 col-md-6 col-sm-12">
   		   	<div class="form-group">
        	<label for="">Email</label>
        	<input type="email" name="email" class="form-control">
      	</div>
        <input type="submit" name="submit" value="Submit">
  	</div>
   </div>
  </form>
</div>

</body>
</html>
Form.html

So the above code is for html part that is been show to user now let get the data by form when user submit the form.

<?php 
if(isset($_POST['submit']))
{
	$name = $_POST['name']; //GET THE NAME OF THE CLIENT AND STORE IN VARIABLE.
    $email = $_POST['email']; //GET THE EMAIL OF THE CLIENT AND STORE IN VARIABLE.
    echo $name. '<br>' .$email;
}
?>

So our output of the php file will be

Output

So we have created verfiy.php file in which we get content by form and display it using echo function.

If you see we have use isset() function what it is ?

So basically, The isset () function is used to check whether a variable is set or not.Here we are using it to check if the form is submitted or not if it is submitted then isset() will return true else false.

So That's It we are done with getting the data by form using $_POST super global variable like $_POST , $_GET , $_SESSION , $_REQUEST and many more.

In the next blog we will understand about super global variable.

Thank you

Abhishek

Hi There! I Am Abhishek Developer, Singer & Photographer. Living In Mumbai. My interests range from technology to entrepreneurship. I am also interested in food, designing, and education.