Simple Forms Parsing
enygma on 2001 April 14
enygma on 2001 April 14
Getting the Information out of Forms with PHP
This tutorial is for the very beginner out there - we're just introducing PHP and how it can be used to get the information out of the forms created with the tags. Now, for those of you still with us, here's how we're going to start. First, the different kinds of form types
text box
checkbox
radio button
drop down lists
multi-line text box
buttons
Now, if you know anything about HTML, this is a review. These items are the basis for most of the interactive content on the web. Forms are responsible for helping web site owners customize your surfing experience and for getting feedback about that experience. They can also allow you to contribute to a site via forums or news postings (like this site). Whatever you use them for, you still need to know how to get the information out of them, and that's where we come in. We're going to show you how to use PHP to grab the info out of these magical little items so you can use that info in your scripts.
First, we'll start with the text box. These are by far the easiest ones to grab the text from because it's a nice simple one line thing. Here's some sample code:
<input type="text" size=10 name="foo">
Now, most will recognise this as a simple text box that's 10 characters long. The cool thing about PHP is that when you have a box like this, once you submit the form, whatever's in the box will be assigned to the variable $foo. Now, what did I tell you - easy, eh? Well, guess what? A large multi-line textbox is almost the same thing. You have your textarea code:
<textarea cols=20 rows=20 name="bar">
that looks like that. Now, it's the same kind of thing. Once the form is submitted, the stuff that's in the box will be put in $bar (even multiline stuff). Simple. Now the fun starts! Before this, we've only had one thing to look at (a single text box), but now we have to worry about multiple things - checkboxes and radio buttons. Now, don't get all scared, it's really not that bad, but it does involve arrays. For those of you that don't know what those are, here's the definition: "an array is a group of objects with the same attributes that can be addressed individually." Now, all this really means is that an array is a group of values associated with each other. I think it'll become more clear once we get into the example:
<input type="checkbox" name=pets[] value="dog">Dog
<input type="checkbox" name=pets[] value="cat">Cat
This code (in your form) will put whatever the values checked are into the array "pets[]". Say the user checked both boxes saying that they liked both dogs and cats. Now, to be able to use this info, we can write a little sample code:
<?php
$number=count($pets);
for($a=0;$a<=$number;$a++){
echo $pets[$a];
}
?>
This little bit of code will print out all of the values in the array. First it gets the number of things in the array with count(). Then, using that for loop, we just run it through each one, with $a being one more than it was the last time through, until they are all done. Now, if for some reason you want to print just one of them (and you know which it is), you can just put the number in:
<?php
echo $pets[0];
?>
That would print the first value in the array (since arrays start at 0). Now, radio buttons are not nearly as bad to handle since you can only choose one of them. Here's a sample:
<input type="radio" name="radio" value="male"> Male
<input type="radio" name="radio" value="Female"> Female
This will print the two radio buttons Male and Female, and since you can only pick one, we don't have to mess with an array this time. The value that they click on will be the value in $radio. See how easy this is? Now, for the last kind of input that the user can give, the drop down list. Now, both kinds of lists are the same thing really, you just set the size to tell the form how many you want seen. Here's an example:
<select name="checks[]" size=5 multiple>
<option value="cats">Cats
<option value="dogs">Dogs
</select>
Now, just like we learned earlier, the array checks[] allows us to know which of the options that the person selects. Since lists can either be drop down or just a regular list (where users can select more than one thing with something like a ctrl+click) we have to worry about multiple selects. Once again, this will put the values selected in the array checks[] and we can use the exact code from above to get the info from it. Now, the last thing to consider in your form is the submit button. This can be anything from the plain old gray button with text to a spiffy little animated gif. That's up to you, but one thing you need to know - the name of the submit button is what matters. You can have all the code on the same page and just use the submit button to set it off. For instance:
<?php
if ($submit){
echo $radio;
}
?>
<form action="< ?echo $PHP_SELF? >" method=POST>
<input type="text" name="foo" size=10>
<select name="checks[]" size=5 multiple>
<option value="cats">Cats
<option value="dogs">Dogs
</select>
<input type="radio" name="radio" value="male"> Male
<input type="radio" name="radio" value="Female"> FeMale
<input type="submit" name="submit" value="Let's do this!">
</form>
In this example, the form and the PHP code are right there on the same page, but the PHP code doesn't execute until the submit button is pressed and the page is reloaded with the $submit variable set. This can help you cut down on the number of actual pages that you have to make also. This page would only print out the value of the radio button that they set, but you get the idea. Well, I hope that this has helped you get an idea of what kind of things you need to worry about when processing forms and when getting user input.
