Contents / Previous / Next


HTML Forms and Feedback (Guest-book Example)

One of the most powerful features of PHP is the way it handles HTML forms.
First, we make the form page, a PHP script with a form in it (could also be a regular HTML) . Example "my_form.phtml": <? echo <<<form_text_region <form action="action.php" method="POST"> Your name: <input type="text" name="name" /><p> Your age: <input type="text" name="age" /><p> <input type="submit"> </form> form_text_region; ?> When the user fills in this form and hits the submit button, the action.php page is called: Hi <?php echo $_POST["name"]; ?>. You are <?php echo $_POST["age"]; ?> years old. // Output of this script may be: //Hi Joe. You are 22 years old. The $_POST["name"] and $_POST["age"] variables are automatically set.
If we used the method GET then the form information would live in the $_GET autoglobal instead.
You may also use the $_REQUEST autoglobal which contains both post and get variables or the import_request_variables() function.


Get versus Post

The difference between "get" and "post" is that the "post" method transparently passes along all the information the page has gathered, whereas the "get" method will pass all that info along as part of the URL.

Get is useful for debuging, because you see which variables with what values you pass to a page and you may set the variables to pass with an external script of by hand (not necessarily with a given form). Data passed with GET is limited in size.
Post is useful to hide variables and their values (e.g., from hidden inputs).