|
| HOW TO PASS DATA AND VALUES BETWEEN PAGES WITH THE GET METHOD $_GET IN PHP
Just like the POST method, the GET method can also be used to pass data and values between pages.
They are two ways you can do this with the GET method:
Using Forms:
Suppose you have the following form on your site:
<form action="nextpage.php" method="get">
<input type="text" name="username">
<input type="submit" value="Send">
</form>
Let's break down the form over a few lines to explain further:
<form action="nextpage.php" method="get">
The action="nextpage.php" part tells the form which page to post the form to when the submit button is pressed.
The method="get" part specifies that the form data must be transferred to the nextpage.php page using the get method.
<input type="text" name="city">
This line displays a text input field. Assume a user types in the word New York and clicks the submit button. A variable $_GET['city'] is automatically created and it's value will be the user's input which was New York. If you echo $_GET['city'] in the nextpage.php page, you will get New York outputted. Note that the variable $_POST['city'] get's it's name from the name of the input text field which is city.
<input type="submit" value="Send">
This displays a submit button.
Note that when the submit button is pressed, the page nextpage.php is opened. Check your browser's address and you will notice that it looks something like this:
http://www.yoursite.com/nextpage.php?city=New York
These details are visible in the browser's address bar and therefore is not as secure as the POST method.
Directly With URLS:
As explained above, the get method appended the name and value pairs to the url which can be retrieved on nextpage.php as below. So, you can also pass values and data directly by appending name and value pairs to the url without using a form.
Example:
http://www.yoursite.com/nextpage.php?city=NewYork&name=John&age=32
The parameter string must start with a ? after the page url. Note that each name/value pair is separated by a &. If you look above, you will notice there are three sets of name/value pairs:
city=New York
name=John
age=32
Each of these values can be retrieved as follows on nextpage.php:
echo $_GET['city']; // New York
echo $_GET['name']; // John
echo $_GET['age']; // 32
 |
|
|