1 Star2 Stars3 Stars4 Stars5 Stars (6 votes, average: 5.00 out of 5)

FORM PROCESSING PHP7 $_POST METHOD CODE EXAMPLES VIDEO LESSON 9

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

Subscribe To My YouTube Channel:

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'); ?>

FORM PROCESSING PHP7 $_POST METHOD CODE EXAMPLES VIDEO LESSON 9 was last modified: November 21st, 2018 by Maximus Mccullough

Advanced Programming Made Easy

I hope you enjoyed the article. If I was able to help you please consider a tip for the content.

One Time Tip

more tips

Reoccuring Tips

Tips

FORM PROCESSING PHP7 $_POST METHOD CODE EXAMPLES VIDEO LESSON 9

Pages: 1 2 Next

Leave a Reply

Your email address will not be published. Required fields are marked *

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