![]() |
|
||||||
In this chapter we’ll cover using forms with PHP. How we retrieve the information submitted through a form (both GET and POST methods) and I’ll also introduce you to multi-dimensional arrays and how we can submit an array through a form. Forms and PHPI briefly mentioned the $_POST and $_GET arrays in the last chapter, in this chapter we’ll see what they’re used for. We can submit a form to a PHP script, let’s take a look at a simple form.That HTML code would create a “form” on your page. The form has two input boxes named “firstname” and “lastname”. Also, look in the <form> tag, the “action” points to “myscript.php” and the “method” is “post”. Meaning the form will be submitted to “myscript.php” using the HTTP request-method POST. So how could we get the information that has been submitted to our script?Let’s assume the user entered “Joe” as their first name and “Blogs” as their last name. If you use the POST form method then all the inputs’ values will be within an associative array, $_POST. If you submit an input named “name” it will be available in $_POST['name'].GETWhat if we submitted the form with the GET method? I’m sure most of you have already figured this out by now, but we’ll go over the example anyway.All we did was change the form “method” to “get”. If we submit a form through the GET method then we use the $_GET array rather than the $_POST array. More on $_GETThe $_GET array is not only useful with forms. Try using the GET form above, when you submit the form look up in your browser’s address bar. You will see something similar to (assuming you are Joe Blogs):The GET form method just sticks everything in the browser address bar, everything after the ? has a name: the Query String. What I’m trying to get at is we don’t only use the $_GET array when using forms, we could simply do this:
Submitting the query string and hence making the variables in it available in the $_GET array. Each item in the query string is split at the & and put into the array for you. This is one of the nice features of PHP. Let’s take a look at another example. Assume we have a script named “print.php”, let’s submit some stuff to it through the GET method.Look at everything after the ?, that’s the query string. It is formatted like: name=value (var “name” has value of whatever “value” is) and each pair is separated by a &. We can now use the $_GET array to retrieve these values in our script.
Have a play around with these arrays, get used to using them.
$_POST and $_GET were introduced in PHP 4.1.0, if you have an older version than this you should upgrade. If you can’t, replace $_POST with $HTTP_POST_VARS and $_GET with $HTTP_GET_VARS. Multi-dimensional ArraysRemember how we defined an array? Which we can then access like: Well instead of just having one value associated with a specific array key, we can actualy assign an array, within an array. So “value1” could actually be an array in itself—creating multi-dimensional arrays. Let’s take a look. That may look a little daunting, but all we did was instead of giving an integer, char or string for an array value, we used another array, so how can we access the items in this array? That’s how you use multi-dimensional arrays. All it is, is an array that has keys that reference other arrays. Why did I show you this? It is possible to submit form inputs as arrays, so instead of $_POST['inputname'] referencing a single value, it can reference an array.Submitting Arrays Through FormsLet’s look at the HTML, how do we make PHP know that the form input submitted is to be an array?All we do is, after the input name (in this case “checkboxes”, follow it with two square brackets. So the name of our inputs that will all be stored in an array wthin the $_POST array is “checkboxes[]”.
That’s easy enough, now let’s look at how we’d get at these values, it’s the same as in the above example. This is the first time we have seen a real function used. We used the count function, all it does is count how many values are present in an array. As we submit our “checkboxes” input as an array, if they check one box $_POST['checkboxes'] would contain one item, if they checked two it would contain two items, and so forth.
If this lost you, don’t worry about it, just try to get variables and simple arrays down. That’s all you really need when you start off, I just showed you this because no one mentioned it to me, I had to figure it out for myself. That about clears up using forms with PHP, remember, if you have any questions just drop me a message, or comment at the bottom. In the next chapter, now that you have seen a function and it’s probbaly making you think “I need to know more about those”, we’ll go onto learn about functions and how we use them.
Comments:
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||