Passing Variables – PHP Post – Fun with Forms

In my last post, we covered the GET method, get is one of two methods used to get information from the client's browser to the server, the other method being "POST." You'll find that "post," by a wide margin, is the method used by developers, over the less secure, "get."

The post method is used when you have a large amount of data in your form, and when you want to be more secure with the data, and not have it so visible, as with the "get" method.

Let's have a little fun and review forms a bit, we'll start with a form the developer might put on a page for the user to fill out in his browser window. Here's the body of the html with the form.

Tell Us about Yourself

My favorite ice cream flavors are (pick all that you like): French Vanilla Chocolate Black Rasberry Vanilla Fudge Strawberry Cookie Dough Coffee What is your favorite pet? Dogs Cats Horses Parrots Snakes I don't like pets What is the make of the current car you drive: Tell us about your favoite hobby and why you like it:

And here is what the form will look like in the browser. I've filled in some example data in the form in the browser window for us to use in the next file. In this case the form action will send the form information to "formctrl.php" which we'll will use to echo out the data.

Here is the php code, minus the php tags, we'll use to retrieve the data in the "formctrl.php" file, and then, in this case, echo out the data to the screen. Notice that the syntax to retrieve the data is the same as we used for "get," except we substitute the word, "post," instead. The data variable in the post portion comes from the "name" tag in the form for each input method.

In the case of asking for ice cream flavors, we selected more than one choice, so we created an array by putting an [] after the name in he checkbox, called, "checkit[]" To retrieve the data from the array, we initialized an array variable in the next file, "$checked," and then use a foreach loop, and go through the array one choice at a time to retrieve the choices.

$fname   = $_POST['fname'];                  // First Name
$lname   = $_POST['lname'];                  // Last Name
$checked = array();
$checked  = $_POST['checkit'];              // Ice Flavors
$radsel   = $_POST['radioselect'];           // Favorite Pet
$dropcar   = $_POST['dropdowncar'];      // Car
$note   = $_POST['notes'];                    // Comments

echo "And now let's check the data from the form.";
echo "";
echo "";
echo "Hello, Mr. ". $lname . ",";
echo "";
echo "Or can we call you, ". $fname ."," ;
echo "";
echo "What are some of your favorite ice cream flavors?";
echo ""; 

foreach ($checked as $ic ){
   echo "I like ". $ic ."";
   }

echo "";
echo "And what is your favorite kind of pet? ". $radsel .".";
echo "";
echo "";
echo "Thanks ". $fname.",";
echo "";
echo "Can you give me a ride?  ";
echo "What kind of car do you drive?";
echo "";
echo "Yes, a ".$dropcar .".";
echo "";
echo "Any thing more you'd like to say?: " . $note  ;
echo "";
echo "OK, What ever you say, Let's go! ";

And here is what the output of the above file will look like in the browser window.

I think from the above example you can see why "post" is the most popular method used with forms, the user's data remains hidden until you specifically get the data in the next file.

We've covered passing the user's information from his browser to a file on the server. We'll next move on to keeping information about an individual user separate from another user, and retrieving it later.

Comments are closed.