Lesson 10: PHP GET and POST parameters

ByDr.-Ing. Erik Neitzel
Lesson 10: PHP GET and POST parameters

This lesson shows step-by-step how PHP inter-script GET and POST parameters are to be used in dynamic web pages. Within WordPress, you see that just by editing a post. Look at your browsers URL when your post editor is open. It says “?post=1008&action=edit&lang=en”. The variables “post”, “action” and “lang” are all variables being filled by WordPress according to what you just clicked in the backend. Let us talk about that.

Why script parameters?

Basically, you could use just one script (file) for your entire website project. However, your code will get rather long, maintenance will get harder and errors more difficult to track. So you may want to exclude certain functionality to other script files. However, every PHP script file works on its own. That means, you will need to transfer data from one script to another. You want to parameterize them.

Based on the purpose of the data being transferred and the origin that data is coming from, there are different types of methods to transfer parameters between PHP scripts. Two of the major methods we are going to discuss here are: GET and POST.

PHP script parameters via GET

GET parameters are coming from the URL, that means the web sites link address within the browser’s address line.

This is how a link within any HTML or PHP file looks like which points to a script.php whilst providing it with a GET parameter.

1
<a href="script.php?var_a=abc123">Anchor text</a>

The parameter’s name is “var_a”, which is completely irrelevant, you can name your parameters as you like, as long as you are able to distinguish between them.

If a user clicks the link above, he will get to the target called “script.php”. Within the file script.php, parameters can be obtained as follows:

1
2
3
<?php
     $your_variable_name = $_GET["var_a"];
?>

In this example, we’re trying to get a parameter called “var_a” and store its value in a variable called $your_variable_name. I would recommend naming that variable the same as as the parameter, but that is not necessary. In the example code above, the variable $your_variable_name will carry the value “abc123”, since that is what the link pointed to.

PHP script parameters via POST

POST parameters are most commonly coming from HTML forms as discussed within the HTML basics article.

The following form will send a parameter named “var_b” to a script called script.php as soon as it is submitted.

1
2
3
4
<form action="script.php" method="POST"><input type="text" name="var_b" /><input type="submit" /></form>

The PHP script script.php now has to do the following to obtain parameter var_b:

1
2
3
<?php
     $your_variable_name = $_POST["var_b"];
?>

Of course you can name the variable storing the parameter’s value the same as the parameter again. In the example code above, the variable $your_variable_name will carry the value the user entered in the input text field.

This is all you need to transfer and collect parameter data between two script files using both GET and POST. You can see that what a CMS like WordPress is doing can be richly complex, yet the underlying structure is never complicated. Otherwise the whole system would suffer under its own weight.

The next lessons:

Dr.-Ing. Erik Neitzel