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
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,
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
Submitting the query string and hence making the variables in it available in the
Look at everything after the
Have a play around with these arrays, get used to using them.
Remember 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
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
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
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.