Showing posts with label Dealing with PHP Forms. Show all posts
Showing posts with label Dealing with PHP Forms. Show all posts

Wednesday, August 8, 2012

Dealing with PHP Forms

PHP: Dealing with Forms:

One of the most powerful features of PHP is the way it handles HTML forms. The basic concept that is important to understand is that any form element in a form will automatically be available to your PHP scripts. Please read the manual section on Variables from outside of PHP for more information and examples on using forms with PHP. Here is an example HTML form:

Example 2.6. A simple HTML form

<form action="action.php" method="POST">
Your name: <input type="text" name="name" />
Your age: <input type="text" name="age" />
<input type="submit">

There is nothing special about this form. It is a straight HTML form with no special tags of any kind. When the user fills in this form and hits the submit button, the action.php page is called. In this file you would have something like this:

Example 2.7. Printing data from our form

Hi <?php echo $_POST["name"]; ?>.
You are <?php echo $_POST["age"]; ?> years old.
A sample output of this script may be:

Hi Joe.
You are 22 years old.
It should be obvious what this does. There is nothing more to it. The $_POST["name"] and $_POST["age"] variables are automatically set for you by PHP. Earlier we used the $_SERVER autoglobal, now above we just introduced the $_POST autoglobal which contains all POST data. Notice how the method of our form is POST. If we used the method GET then our form information would live in the $_GET autoglobal instead. You may also use the $_REQUEST autoglobal, if you do not care about the source of your request data. It contains the merged information of GET, POST and COOKIE data. Also see the import_request_variables() function.

User contributed notes:

RobertMaas at YahooGroups dot Com
Regarding debate over GET and POST method: One disadvantage of the POST method is that you can't bookmark it in a URL. So if you want ot make a URL that bookmarks both the WebPage and some form contents, you have to use the GET method, i.e. webpage?formcontents. Warning, the Apache server logs the complete URL including formcontents in a file that anyone can read, so be sure never to include a password in the form in GET method, regardless of whether bookmarked or onetime. For example, my CGI site has a login form, and I have a bookmark for specifying my own (rather long) e-mail address, but do *not* include my password in that bookmark, rather I click on the link and get the login form with my e-mail address already filled in but I still have to type my password, and of course it's a POST instead of GET form when I then submit it with password.
(Cliki complained it was too long so I had to split it.)

pamela at alaskapam dot com
Using two pages for the examples created the output from the first page (Example 2-6. A simple HTML form) on the second page (Example 2-7. Printing data from our form). Then I combined both examples on the action.php page.   When I refreshed the page, I was asked to resend the information which showed the previous entries in the form.  Inputting a new name and age, then submitting query resulted in the form showing the new data.  
I preferred the two-page way of handling this tutorial example.

JohnD717 at hotmail dot com
GET and POST methods: The Get and Post methods in a form is the way the information is passed to the processing script. In PHP these are simply put into $_GET['<name>'] or $_POST['<name>'] however in other languages, like perl/cgi, it is handled differently. if you are writing a generic form for any processor, i recommend post. It is not only easier in languages that handle them differently, but in PHP, POST is the assumed method by more script writers. Also, in PHP, $_GET is used for the query string as well, and you don’t want to end up having duplicate names in the query string and the form.