“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.

Only cool people share!

$_GET Method Attribute Example

[code]<form method=”get”></form>[/code]

$_POST Method Attribute Example

[code]<form method=”post”></form>[/code]

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.

[code]<form method=”get” action=””></form>[/code]

In this example the action attribute is telling the HTTP protocol to go to a different page called “processing.php” to process the form.

[code]<form method=”get” action=”processing.php”></form>[/code]

$_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

[code]<form method=”post” action=””[/code]

Processing On A Different Page, Processing.php

[code]<form method=”post” action=”processing.php”></form>[/code]

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]<?php if(isset($_POST[‘foo’])){echo $_POST[‘foo’];}?>[/php]

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]<?php echo $_POST[‘foo’] ?? ”; ?>[/php]

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]<?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”>
<input type=”submit” class=”btn btn-success btn-lg” value=”Submit”>
<div class=”col-sm-6″>
<?php include(‘footer.php’); ?>[/php]

processing.php Code Example

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

myform.php Example When Submitting To Same Page

[php]<?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”>
<div class=”col-sm-6″>
<?php $fname=$_POST[‘fname’] ?? ”; ?>
<?php lastName($fname); ?>
<?php include(‘footer.php’); ?>[/php]

FORM PROCESSING PHP7 $_POST METHOD CODE EXAMPLES VIDEO LESSON 9 was last modified: January 2nd, 2023 by Maximus Mccullough

Pages: 1 2 Next

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.