Working with forms in PHP - A simple form
(Page 2 of 7 )
OK, let's start off with a very basic form. Just a single button.
<HTML> <HEAD> <TITLE>A simple form</TITLE> </HEAD> <BODY> <FORM method="POST" action="form.php"> <INPUT type="submit" name="mybutton" value="Click me!"> </FORM> </BODY> </HTML> |
OK, put that into a file and call it whatever you want. I will be calling it form.html. That will give you a very basic form with one button on it. You can see the "action" of the form is "form.php". That means that whatever data the form contains will be sent to the script form.php. The next step is to create the script form.php.
<HTML> <HEAD> <TITLE>Doing something with the form</TITLE> </HEAD> <BODY> <? if(isset($_POST['mybutton'])) { echo "Great! You clicked the button!\n"; } else { echo "Hmm...you must have come to this script without clicking the button.\n"; } ?> </BODY> </HTML> |
Save that code as form.php in the same directory as form.html. Now when you bring up form.html and click the button, you should get a message saying "Great! You clicked the button!".
Let's examine the code in form.php a little bit. We are going to be using this code for the basis of everything else in this tutorial. The first few lines are self explanatory. They are just the HTML needed for a proper HTML page. The first code is:
if(isset($_POST['mybutton'])) { |
What that is doing is checking to see if the variable $_POST['mybutton'] has been assigned a value or not. If it has, the "if statement" evaluates as true and then the line:
echo "Great! You clicked the button!\n"; |
is executed. If the "if statement" evaluates as false then the "else" portion of that statement is executed:
echo "Hmm...you must have come to this script without clicking the button.\n"; |
So what is this $_POST stuff? $_POST is a superglobal array. This array is available to a script at anytime no matter what the scope. If you aren't sure what scope is, don't worry about it right now. Just realize that you can use $_POST anywhere in your script. Anything submitted via the POST method is available in the $_POST array.
You may have noticed that the index of the $_POST array is the name we gave to the HTML form element. Whatever you call your form element (a form element being a button, textbox, checkbox, etc), that is what the index of the $_POST array will be in order to access the data from that element.
The $_POST array is only available in PHP versions 4.1.0 and later. If you are using an earlier version, you will need to use the $HTTP_POST_VARS array. The $HTTP_POST_VARS array works the same as the $_POST array, except it is not a superglobal. That is, it is not available in all scopes. In fact, if register_globals is turned off in your php.ini you will need to put "global $HTTP_POST_VARS;" at the top of the form.php script for it to work.
Next: Text Boxes >>
More Miscellaneous Articles
More By Matt Wade
|
| · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | · | | | | |
|