HTML Input Forms – Sending in an array in PHP

Arrays? Who needs ’em?

If you’re into developing websites chances are there will be sometime during your life/career when you’ll need to have users enter data into a HTML form but you have no idea how many of a certain variable they’re going to be sending in or how much data they’re going to fill in of the same type.

An example would be a HTML form that has three input boxes, one for each of your friend’s names (first and last). You are able to enter between 1 and 3 friends into the boxes. For such an example your HTML form may look something like this:

<form method="post" action="">
    <p>Enter your friend's names (first, last):</p>
    <input maxlength="30" name="friend1" size="30" type="text" />
    <input maxlength="30" name="friend2" size="30" type="text" />
    <input maxlength="30" name="friend3" size="30" type="text" />
    <input type="submit" value="Submit" />
</form>

That all looks fairly normal. Keep in mind this is a simple example. But, what about if you’ve got the option to enter 10 friends. What about 20? You might have to re-work your form if you want to enter 20 friends. Any more than 10 and you might want to look at alternatives like importing from XML or CSV.

Let’s say you’ve got the option to enter up to 10 names. Your increasingly ugly form would then look like this:

<form method="post" action="">   <p>Enter your friend's names (first, last):</p>   <input maxlength="30" name="friend1" size="30" type="text" />   <input maxlength="30" name="friend2" size="30" type="text" />   <input maxlength="30" name="friend3" size="30" type="text" />   <input maxlength="30" name="friend4" size="30" type="text" />   <input maxlength="30" name="friend5" size="30" type="text" />   <input maxlength="30" name="friend6" size="30" type="text" />   <input maxlength="30" name="friend7" size="30" type="text" />   <input maxlength="30" name="friend8" size="30" type="text" />   <input maxlength="30" name="friend9" size="30" type="text" />   <input maxlength="30" name="friend10" size="30" type="text" />   <input type="submit" value="Submit" /> </form>

When you submit this form then you’ve got to check each input box to first ensure they entered a variable and then check to see what that variable is. With so many input boxes to check from it becomes a repetitive and arduous task.

Example PHP code:

// Good God.. okay let's start this horrible task
if ($_POST['friend1']) {
    doSomething($_POST['friend1']);
} else {
    complain();
}
if ($_POST['friend2']) {
    ...
} else {
    complain();
}
if ($_POST['friend3']) {
    ...
} else {
    ...
}
...
...
// There's got to be a better way!

Save time with arrays.

Using PHP and HTML, the HTML form code can be re-written to have the server create a PHP array of your friends’ names, like this:

HTML Form:

<form method="post" action="">
    <p>Enter your friend's names (first, last):</p>
    <input maxlength="30" name="friend[]" size="30" type="text" />
    <input maxlength="30" name="friend[]" size="30" type="text" />
    <input maxlength="30" name="friend[]" size="30" type="text" />
    <input maxlength="30" name="friend[]" size="30" type="text" />
    <input maxlength="30" name="friend[]" size="30" type="text" />
    <input maxlength="30" name="friend[]" size="30" type="text" />
    <input maxlength="30" name="friend[]" size="30" type="text" />
    <input maxlength="30" name="friend[]" size="30" type="text" />
    <input maxlength="30" name="friend[]" size="30" type="text" />
    <input maxlength="30" name="friend[]" size="30" type="text" />
    <input type="submit" value="Submit" />
</form>

Server-Side PHP:

// Loop through the friend array
foreach ($_POST['friend'] as $value) {
    // Do something with each valid friend entry ...
    if ($value) {
        echo $value."<br />";
        ...
    }
}

Let’s walk through that PHP code.

It’s fairly simple. We walk through the php $_POST[‘friend’] array that we retrieved from the HTML form using foreach and for all of the entries on the form that the user typed in we do something with (in this case we simply echo them to the screen).

This simple foreach loop will save you time and the good part is that you can have any number of friends on your form (I’m sure there’s a maximum somewhere though.. 256 maybe?) and this block of code will still work.

In the next article I’ll show you how to do the same thing with ColdFusion.

19 thoughts on “HTML Input Forms – Sending in an array in PHP

  1. found your site on del.icio.us today and really liked it.. i bookmarked it and will be back to check it out some more later ..

  2. turn the lights on here please, its killing my eyes lol. Nice tut tho.

  3. very very helpful but your website design is cool except for the colors u hv used. they r also killing my eyes

  4. I’ve just tried this, and it works fine except it doesn’t work with my file upload fields, any suggestions?

  5. Never mind; I solved it; I was trying to access it with $_POST[‘file’][0][‘Name’] instead of $_POST[‘file’][‘Name’][0]

  6. /*
    HI, Please any one can give me this solution.
    I need to take multiple input value using while loop, and using post I will show data of those input field.
    */

    Name :
    Unite :
    Friend :

  7. @haris

    You can use the for loop to print multiple values

    eg

    $Name = $_POST[‘Name’];
    $Unite = $_POST[‘Unite’];
    $Friend = $_POST[‘Friend’];

    for($i = 0; $i<count($Name); $i++){
    echo $Name[$i].'’;
    echo $Unite[$i].”;
    echo $Friend[$i].”;
    }

  8. how would you do this by entering one element of the array at a time int he html form, submitting it, and then entering the 2nd, etc.

  9. Hmm I’m getting an error Warning: Invalid argument supplied for foreach() in … on line 17

  10. I did the code below so if the array you wanted was empty if wouldn’t give you arrays you don’t need.

    $amount=$_POST[‘paid_amount’];
    $id=$_POST[‘loan_id’];
    for($i = 0; $i<count($amount); $i++){
    if ($amount[$i]!="") {
    echo $amount[$i]."”;
    echo $id[$i].””;
    }

  11. How to send a mail, all checked checkbox values in my form via PHP.

  12. how to add same form field values in to an array without pull the adding elements

Comments are closed.