Hey everybody I want to let you know that I have undertaken the grueling task of getting the heck away from WordPress. I was so sick of the problems and updates I had to do all the time. I am now using my ezbloo system and I am integrating all my old posts into the new system. It sucks, but in the end, I will save bundles of time. I needed to keep things simple and that is why I created ezbloo. I'll have more on this later for you guys after I am done with the total integration of my old posts here. So if you are looking for a post and need it faster, shoot me an email and I will make it a priority. [email protected]

"Form processing" in PHP7 using the $_POST method is much like using the $_GET method. In this video lesson on using the $_POST method, I will show you some code examples. In programming code, you will be using the $_POST method more than the $_GET method. Here are the files from our last lesson. Here are the files for our complete lesson today.

In Form Processing, What Is The Difference Between $_POST and $_GET?

The difference between $_POST and $_GET in form processing is that $_GET  reveals the "parameters" in the URL string. The $_POST method carries the "parameters" in the body, thereby making it more secure. Another difference is that you cannot upload files with the $_GET method, you must use the $_POST method for this process. 


Using The Method Attribute In A Form Tag For $_POST And $_GET

We use the Method Attribute to let the HTTP protocol what kind of method we are using. The method by default is $_GET. You will notice in the last lesson we did not use the method attribute because this is the default behavior. To use the method attribute in our form tag we would insert the method attribute. Here is an example.

$_GET Method Attribute Example

<form method="get"></form>

$_POST Method Attribute Example

<form method="post"></form>

The "Action" Attribute in $_POST and $_GET

The "Action" attribute in $_POST and $_GET defines where the form is going to go. By default the form will be submitted to the same page the form is on. You can enter another page to process the form in the "Action Attribute". You will notice in the last lesson we did not define an action in our form tag. If we combine the "Method Attribute" with the "Action Attribute" the opening form tag would look like this.

$_GET Method and Action Attribute Example

This first example the action instructs the form to stay on the same page the form is on. This is the same as having no action defined.
<form method="get" action=""></form>
In this example the action attribute is telling the HTTP protocol to go to a different page called "processing.php" to process the form.
<form method="get" action="processing.php"></form>

$_POST Method and Action Attribute Example

When using the $_POST method, everything will be the same as the $_GET method. The only difference would be in the "Action Attribute" where you define where the form is going to go.

Processing On The Same Page As The Form

<form method="post" action=""

Processing On A Different Page, Processing.php

<form method="post" action="processing.php"></form>

What Is Better?

Personally, I believe it is better to process forms on the same page the form is on when coding like this. It is a more secure way to code.

How To Work With Post Variables in PHP7

There are 2 ways to get the post variables. Think of the variables like parameters, the only difference is you cannot see them in the URL string when using $_POST. Here are the 2 ways of getting $_POST variables.

Old Way Of Working With $_POST Variables

By the way, this still works and I use it often.
<?php if(isset($_POST['foo'])){echo $_POST['foo'];}?>

New Way in PHP7 Working With $_POST Variables

The new way is a lot less coding of course. Less to remember as well. This is called the null coalescing operator (??), this was introduced in Php 7.
<?php echo $_POST['foo'] ?? ''; ?>

Code Examples Used In Video for $_POST Methods in PHP

In the video, I created 2 new pages in our website project. One is myform.php and the other is processing.php.

myform.php When Submitting to processing.php Code Example

<?php include('header.php'); ?>
<div class="row">
<div class="col-sm-6">
<form method="post" action="processing.php">
<div class="form-group">
<label for="fname">First Name</label>
<input type="text" class="form-control" name="fname">
</div>
<input type="submit" class="btn btn-success btn-lg" value="Submit">
</form>
</div>
<div class="col-sm-6">
</div>
</div>
<?php include('footer.php'); ?>

processing.php Code Example

<?php include('header.php'); ?>
<p><?php echo $_POST['fname'] ?? '' ?></p>
<?php include('footer.php'); ?>

myform.php Example When Submitting To Same Page

<?php include('header.php'); ?>
<div class="row">
<div class="col-sm-6">
<form method="post" action="">
<input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</div>
<div class="col-sm-6">
<?php $fname=$_POST['fname'] ?? ''; ?>
<?php lastName($fname); ?>
</div>
</div>
<?php include('footer.php'); ?>

Conclusion on $_POST Method

I hoped you enjoyed the tutorial.

Please share it with your friends and subscribe. If you have any questions, comments or concerns, please comment below. Have a great day! :-)