A Closer Look At The Post Method
If you've been following along then you will have already used Trongate's in-built post() method. Now, let's take a little time out and explore precisely how this method works. For those of you who prefer videos, here's everything you need to know:
In this video, we'll take a closer look at how Trongate's post() method works.
For those who prefer written docs, let's take a moment to compare pure PHP with Trongate's post() method.
Understanding How Pure PHP Fetches Posted Data
Let's assume that a user has submitted a form with a field called 'title'. In pure PHP we can fetch the posted title value by accessing a special kind of variable, in PHP, called a superglobal. The syntax for fetching a posted 'title' field, in pure PHP, would be:
$title = $_POST['title'];
By adding a var_dump() onto our code, we can take a closer look at what has been submitted. For example:
$title = $_POST['title'];
If a user then submitted a title of 'here we go', the following message would be sent to the screen:
No surprises. So, let's take a closer look at how Trongate's post() method works.
Understanding How The post() Method Fetches Data
Let's repeat the exercise above, only this time we'll use Trongate's post() method, instead of pure PHP.
$title = post('title);
Once again, if a user were to submit a title value of 'here we go', then we'd have the same message sent to the screen as we had with pure PHP.
With this being the case, you'd be forgiven for thinking that PHP's $_POST superglobal and Trongate's post() method did precisely the same thing. However, the key differences between these two things are revealed when we check for values that have not been posted.
For example, let's use pure PHP again. This time, we'll check for a posted value of 'banana'.
$banana = $_POST['banana'];
Since we have not posted a form field with a name of 'banana', pure PHP will produce an error message:
However, if we test for the posted 'banana' field using Trongate's post() method, we get a completely different result:
$banana = post('banana');
This time, we don't get an error message. Instead a 'banana' variable will be initialised automatically. Our new 'banana' variable will be an empty string.
This difference is subtle. However, it's something that we can take advantage of when we're building forms using Trongate. In the next page you'll see how.
The Optional 'true' Argument
When you invoke Trongate's post method, you have the option of adding a second argument - a boolean of true.
Here's an example:
$title = post('title', true);
Passing in a second argument of true does two things:
- It runs the fetched value through PHP's trim() function to remove whitespace from both the left hand side and right hand side of the string
- It runs the fetched value through PHP's strip_tags() function. This removes tags (i.e., pairs of less than and greater than symbols) and the content within tags. Please refer to the video, above, for a demonstration.
HELP & SUPPORT
If you have a question or a comment relating to anything you've see here, please goto the Help Bar.