Search
Left Quote    I think there is a world market for maybe five computers.
- Thomas Watson, Chairman of IBM 1943    
Right Quote
 
[login] | [Register]
 

Introduction to PHP

by: Jester
Page: 4 of 7
(View All)

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 PHP

I 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'].

GET

What 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 $_GET

The $_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.

    Note:
$_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 Arrays


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 $_POST['inputname'] referencing a single value, it can reference an array.

Submitting Arrays Through Forms

Let’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.



1  |  2  |  3  |  4  |  5  |  6  |  7  |  
Next »


Comments:

  guru
  Subject: "Multi-dimension al Array is not wor..." Date: Dec 01 2007 at 7:38 pm    
I had tried with multi-dimension l array in PHP using your script. But it is not working. Is it my problem? Or Any correction needed to you?
  tannenba
  Subject: "re: multi dim array" Date: Dec 09 2007 at 8:10 pm    
Seems to work for me. Typo guru?
  moonshadow
  Subject: "Multi-dimension al" Date: Feb 26 2008 at 9:04 pm    
I try to run it with ie and foxZilla and it didn't work, but I open a php program and put the script and it worked.
You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

320,094 Views
9 Total Comments
5 Rating of 5 (2 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Simple PHP Tutorial
· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope



"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank