This will be a rather short exercise, but it presents a major component of PHP: processing form data. Up to this point all we have done is generate HTML output using PHP code. Nice, but kinda boring. Now we will begin to generate HTML output using PHP code that is based on input from a user in the form of a form or URL. This is the first step in creating dynamic code.
There are two ways to pass data from a client's browser to the server:
This lab will look at the methods for getting that data into your PHP scripts.
By the way, for those of you doing the reading, this material comes from the Williams and Lane book, Web database Applications with PHP and MySQL, pages 189 to 202.
Okay, so this is the second forms refresher this semester, but since the first one was over two months ago, I thought it might not be a bad idea to just hit the high points. Furthermore, we're going to have to make a table to play around with our PHP scripts anyway.
Just as with JavaScript, we are going to be accessing the form data using the names of the forms and each of their elements, so be sure to use the name/id attribute for all components of the form.
Remember that a form tag has three attributes:
The attribute action specifies the program or URL that the form will be sent to. The attribute method tells the browser how to get the form data to the server applications. There are two values for this attribute: "get" and "post". Setting method to "get" sends the client's data as part of a URL just like you were typing the URL to a web site. (Note that this method is what we will be mimicking in order to send PHP data as part of a URL.)
Setting method to "post" sends the client's data to the server as part of a separate file.
Last of all, the target attribute tells the browser in which window to display the results or if a new browser window should be opened to display the server's response.
The table below is presented as a brief (incomplete) reference for the table elements. For a better reference, see Devguru's descriptions of the form tags input, select, and textarea.
Doing it on your own At this point we simply need a table that will allow us to send data to the PHP scripts. Create a table with four input fields, each having a width and size set to 15. Use the following name/id attributes to identify the individual fields.
Remember to include a submit button!For the form tag, use a method of "post" and an action of "http://einstein.etsu.edu/~tarnoff/al.php" In order to facilitate the use of your table, you might want to identify the purpose of each of the text fields to the client. You can use the image below as a guide. The singular verb, by the way, should be like the dog barks or Bob types. Once you have completed your form, click submit and see if it accesses my PHP script correctly. |
We are going to take small steps toward the development of a PHP script designed to accept form data. The reason for this is that if we create a large piece of PHP code and try to run it all at one time, it will be very hard to debug using our PHP engine's limited capabilities for debugging. This is a good rule to follow whenever developing any type of code.
Doing it on your own First, we will simply see that we can get a PHP script to open as a result of using it as the action of a form.
|
Now what we need to do is be able to get the text strings from the form inputs incorporated into our script. The data from a form is passed to the PHP script as part of an array. The form element's name is the argument for the array. For the method "post", the array name is $_POST while $_GET is the array name when the method "get" is used.
Doing it on your own First, let's see this array that is sent to the PHP script. Somewhere in
the PHP script contained in your "adlib.php", insert the PHP
function Okay, now that we've seen the array, delete the "print_r" function and begin replacing the elements of the nursery rhyme. Replace the word "muffin" in our nursery rhyme with the form element "food". To do this, point to the form element using the PHP array element $_POST["food"]. Do a search on "muffin" and replace with "{$_POST['food']}". Open your form again, enter a value for the food, and press the submit button. Did it work? If so, replace "man" with the form element for "animal", replace "lives" with the form element for "verb", and replace "Drury Lane" with the form element for "street". |
If we had wanted to do this same exercise using the method "get", all we would have had to do is replace the array $_POST with $_GET.
Now let's see how form elements other than inputs of type "text"show up in the PHP arrays $_GET or $_POST.
Doing it on your own From the XHTML template, create a form with the following elements and their characteristics:
Use as the form's action "print_r.php". My final table looked like the following: Now we need to create the script print_r.php. Use the PHP template to create a file named "print_r.php" In the script, simply have the command "print_r($_POST);" This should print out all of the elements sent to the script. Once you've save it, run the script and see if you can answer the following:
|
Run this form multiple times to see how modifications in the entered values show up in the script. Be sure to execute the form both with a checkbox selected and with it not selected. In the case where the checkbox is not selected, that particular value does not show up in the PHP data. In other words, the client will only send data for checkboxes that are selected. This means that in your programs you will need to take special consideration for the code used to check checkboxes. It should be something like:
if ($_POST["check_value"] == "true")
// insert code here for the "true" case
else
// insert code here for the "off" case
There is no reason to test for a checkbox being off. It will never work. In addition, the value "true" is simply the value I used for the checkbox. If you used a different value for the checkbox, that will be sent to the script instead.
This matter of the checkbox data not being sent if nothing is checked becomes a real problem when the checkbox is the only form element. This means that there will be no $_POST or $_GET array since there will be no data at all. To avoid this, include a hidden element or name the submit button so at least some data is sent.
A recent addition to the attributes of the select input is "multiple". It allows a client to select more than one element from a select/drop down box. The select box below uses this attribute. Holding the shift key or the ctrl key down when clicking options allows for multiple selections.
By the way, the code for the above selectbox is:
<select multiple="multiple" name="colors" >
<option value="0">red</option>
<option value="1">blue</option>
<option value="2">green</option>
<option value="3">pink</option>
<option value="4">teal</option>
<option value="5">other</option>
</select>
Although most designers opt for checkboxes when selecting more than one option, the option to allow for multiple selections from a select element adds an interesting facet to receiving form data. When PHP receives the values selected by the client, only the last value is visible. For example, if we were to do a print_r() of the data received from the above select box when red, blue, and pink were selected, the result would be:
Array ( [mult_example] => 3 )
where 3 is the value assigned to pink.
To avoid this, name the select element with a PHP array-styled name, e.g., colors[]. When the array is initialized, it will create an array of selected values for that form element. If we were to change the name of our select to "colors[]", we would get the following result from our print_r() function.
Array ( [colors] => Array ( [0] => 0 [1] => 1 [2] => 3 ) )
If needed for other form elements, the array-styled name can be used allowing subsequent form values to be included in an array.
GET and POST information are not the only things that are available with arrays. You can access a number of blocks of information such as:
Doing it on your own Enhance your print_r.php script with the following print_r statements. You may want to add some HTML code between the statements to sort out which array is being printed out.
What information can you pull from these arrays? Can you see how any of it might be helpful? |
Now that we can receive input from a form, let's make sure it is formatted properly. The in-class exercise from the lecture asked us to use the string functions to filter, refine, and pull apart strings using the functions:
Using these functions, create a simple form with two text inputs, one for a phone number and one for an e-mail address, and a textarea to input a string. Use as the action of the form the URL "http://einstein.etsu.edu/~zabc123/strings.php" where zabc123 is your user name. Next, using the PHP template, create a script that will:
Developed by David Tarnoff for his students in CSCI 2910 at ETSU