Languages - PHP
GET and POST

There are 2 ways to pass form parameters back from a web page

Overview | GET example | POST example | Some Functions | Radio buttons | Checkboxes


Overview

Both $_GET and $_POST are super globals - meaning that they are available at all times, in any subroutine, without having to declare them as globals. In all cases, you should never assume that a parameter exists. To prevent errors like you should always test for the existence of a parameter before attempting to use it. I tend to develop forms using get (because it is easier to debug what you can see), and to publish them using post (so the users can't manipulate them and break something). However, the debug parameters are always retrieved using get so that I can control those manually (ie, type them in). In order to make it difficult for others to see your debug stream, you can


GET example

With Get, the parameters are visible in the url. PHP code that creates and reads the parameters


POST example

With Post, the parameters are not visible in the url. PHP code that creates and reads the parameters


Some Functions

It is absolutely required (or at least - good practice) to make sure that the variable is defined and that the input parameter exists. If you just need a value, the following functions perform the appropriate tasks. With the 3rd function, you won't have to change your code when you just change the form parameter.


Radio buttons

All the buttons in a radio group have the same name. The value returned indicates which button is selected. This example keeps the current radio button checked when the form is recalled. Using the functions defined above, the following would simplify handling radio buttons.


Checkboxes

Check Boxes are not associated into groups. In these cases, the parameter exists only when the box is checked. This example keeps the keeps the state when the form is recalled. Using the functions defined above, the following would simplify handling checkboxes.


Author: Robert Clemenzi
URL: http:// mc-computing.com / Languages / PHP / Get_Post.html