Mrarrowhead.com

 
   

Sign up for FREE PHP Newsletter

Learn basic syntax, experiment with loops, and build user areas.

Name:

Email:






 
 

Passing arrays to another page

Author: D.Shaun Morgan

Versions and Skill Level
  • PHP Version - PHP 5x
  • Reader skill level - Beginner
  • XHTML 1.0 Transitional - interchangeable with HTML 4.0
See Also:
  1. How can I pass a PHP variable from one page to another

In PHP programming, just like other languages such as c and Python, you will use variables to pass along input from a user to a script that will take that info and do something with it. In PHP this input will most likely come from a form on a website. The reason to use a variable, of-course, is that there may be more than one possible input for that particular problem. A person's name, is one example. So, you create a variable called "$first_name." A single variable like "$first_name" works just fine for a script that only handles one instance of the variable at a time. But, what if you have a form or other input that will handle 5 instances of the input "first_name"? You could make five different variables and send them to your script -- array_script.php.

Form example No. 1.

<form method="post" action="array_script.php">

0.<br/>
<input type="text" name="first_name0"/><br/><br/>

1.<br/>
<input type="text" name="first_name1"/><br/><br/>

2.<br/>
<input type="text" name="first_name2"/><br/><br/>

3.<br/>
<input type="text" name="first_name3"/><br/><br/>

4.<br/>
<input type="text" name="first_name4"/><br/><br/>

<input type="submit" name="submit"/> </form>

<?php

$first_name0 = $_POST['first_name0'];

$first_name1 = $_POST['first_name1'];

$first_name2 = $_POST['first_name2'];

$first_name3 = $_POST['first_name3'];

$first_name4 = $_POST['first_name4'];



?>

That's not too bad really, but the number could be much larger. What if you are coding for something like a customer database where variables may get into the thousands or millions? The task of typing a million variables would be impossible. Lucky for us, the good folks who invented PHP predicted this problem early on and gave us arrays.

Below is an example of how to pass an array from an HTML form using the POST method.

Take Form example No. 1.

<form method="post" action="array_script.php">
0.<br/>
<input type="text" name="first_name0"/><br/><br/>

1.<br/>
<input type="text" name="first_name1"/><br/><br/>

2.<br/>
<input type="text" name="first_name2"/><br/><br/>

3.<br/>
<input type="text" name="first_name3"/><br/><br/>

4.<br/>
<input type="text" name="first_name4"/><br/><br/>

<input type="submit" name="submit"/> </form>

Now, change all the "name" attribute values to "first_name[]."

Form example No. 2

<form method="post" action="array_script.php">
0.<br/>
<input type="text" name="first_name[]"/><br/><br/>

1.<br/>
<input type="text" name="first_name[]"/><br/><br/>

2.<br/>
<input type="text" name="first_name[]"/><br/><br/>

3.<br/>
<input type="text" name="first_name[]"/><br/><br/>

4.<br/>
<input type="text" name="first_name[]"/><br/><br/>

<input type="submit" name="submit"/> </form>

In HTML forms with PHP, much like other web scripting languages, variable arrays use "[]" brackets to indicate that the variable is an array. The brackets are also where you place the key that you will use to access a certain value. If I send (Form example No. 2.) to my PHP script without the brackets attached to "first_name," I will only gain access to the value sent in text-input box number four. If I send a, b, c, d, e, I will only get "e" from my script.

This time when my PHP form gets submitted to the script, all the text that I typed into the HTML input text boxes will be saved as a single array in the $_POST super-global associative array, accessible by what I named it in the "name" attribute of my PHP HTML form. Like this: $_POST['first_name']. The following script is an example of how to access and use this array.

To clarify what I just typed: If I send stuff to my script via (method="post") with my HTML form, when I'm ready to use it in my script, the code will look like "$_POST ['first_name']. " If I use get instead of post, I will type ($_GET['first_name']).

Here is a simple script to access all the values I stored in the variable array "first_name[]."

<?php

$first_name = $_POST['first_name']

echo $first_name[0].'<br/>';
echo $first_name[1].'<br/>';
echo $first_name[2].'<br/>';
echo $first_name[3].'<br/>';
echo $first_name[4].'<br/>';


?>

This will output to screen the values entered in (Form example No. 2). If I enter a, b, c, d, e into (Form example No. 2), the output to screen generated by my PHP script will look like this.

  1. a
  2. b
  3. c
  4. d
  5. e

Click here for a visual model PHP $_POST.

Notice that the numbers enclosed in "[]" brackets match the numbers that I used above each text box in the form. When PHP received "first_name[]" from my form, it recognized "first_name" to be an array. "first_name" was turned into a single dimension numeric array.

Here is a tid-bit of info, just in case you are thinking, "won't I have to type out a thousand separate instances in the "name" attribute for the HTML input?" Here's the trick -- I can also use PHP and/or other scripts and programming languages, like Javascript, to generate thousands of input boxes. That way I would not actually have to write the markup for the text boxes. My script would do it for me. Let's take a look at how to do this with a basic script -- a PHP loop, and HTML

<?php

for($x=0;$x<4;$x++){

echo 'text-box $x <br/>

<input type="text" name="first_name[]"/><br/><br/>';

}

?>

The above PHP script will output five input text boxes.