Category: php

.

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 (not sure why I would need a thousand!). 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<5;$x++){

echo "text-box $x <br/>

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

}

?>

The above PHP script will output five input text boxes.

Author: D.Shaun Morgan

passing data from one form to another form php Passing data from one form to another form To pass php data from a form and then catch that data into another form one way to accomplish this task is to intermi

how to turn variable value into variable name Variable Variables in PHP Every so often when you are writing a php file, we all run into a situation where we would like for the name of the string that is inside

notepad plus Notepad++ Download Notepad Plus Plus Notepad plus plus is an open source, free text editor. It is

how to make money online php Making Money on the Internet Sign up for this series of free money making articles Name:

store images mysql php php Storing Images in Mysql Database Tutorial Outline: Creating a Test Mysql Database Example php Database Functio

.
 

Leave a Comment

php_arrays.php

Name:      | Email:      | Website:

Comment:


captcha

Enter The Above Security Code:


Author: Kevin Davis | Website URL: http://kevincdavis.net/ |
Comment:
Nice script, but I do have a quick question. How I can make it work in where number of fields I need is unknown. Say for example someone needs at least 10 fields instead of 5, how can I make it work that way?

Re: From Admin:

If I am understanding you correctly, what you are wanting to know is how to deal with an array of variables that could have variable changing amount of data in it.

If, however, you need a webpage that offers your visitors a variable amount of input text boxes, you will want to write a script and a page that asks the user if he wants to add another box or field. It might look somethign like this:

<?php>

session_start();
if($_GET['anotherBox'] == "y"){
$newbox = "<input type='text' name='anotherBox[]' value='' /> <br/><br/> "
} $_SESSION["$AllBoxes"] .= $newbox;

Then further down in the page you can tell the all boxes variable to dynamically produce another input box every time your user chooses to create one.

<form action="" method="get" > <br/>

<php echo $_SESSION["$AllBoxes"]; ?>
<input type="checkbox" name="anotherBox" value="y"/> <br/><br/>
<input type="submit" value="Submit" /><br/><br/>
</form>

After the user fills in his buy web hosting or other eccomerce form and puts in all the data he needs, there will exist on the webserver where you went to buy hosting and but a domain domain anem a $_GET variable array called $_GET['anotherBox'] that will hold the data from the dynamic form. In this case you will use something like a "for loop" to use the data in that variable
Author: Roberto Molina | Website URL: |
Comment:
Hello I have other situation and I need to pass a lot of data from several pages or Formula maybe 8 to another with all the data. Let me give a Example:

We have one Formula with, name Age Address and a small calculation, and then I want to have only one report with all these data. That means all the 8 People in only one Table. The formula is building with radio button and text Box. I can do only with one formula and one report, but with 8 fields to connect with one Report I don't have any idea. Many thanks.
Author: developer99 | Website URL: http://developer99.blogspot.com |
Comment:
hi, you can pass array through two methods. by input values or by json,, i hope that this post will help you http: developer99.blogspot.com 2011 07 passing-array-from-one-page-to-another.html
Author: ward | Website URL: www.telerex-europe.com |
Comment:
Hello, How do I do the complete opposite of this tutorial? I have a given number of values out of a select , generated by a FOR loop: Through this I m able to generate a string with DOCUMENT.WRITELN, but I want to get this string into a TEXTBOX, without leaving the page... After completing the form which has more values to enter I want to pass this TEXTBOX value as a single string to the database... function selIt { var pickList = document.form1.elements[ list2[] ] var pickOptions = pickList.options var pickOLength = pickOptions.length for var i = 0 i pickOLength i++ { document.writeln pickOptions[i].value } return true } How can this be done? Thanks in advance, Bye Ward
Author: Tupa | Website URL: |
Comment:
This code:

for $x=0 $x 4 $x++ {}

Actually output four input text boxes, not five.


Re: Admin


I'm certain that it does output 5


Re: Admin


Actually you caught it Jeffry. It does now create five textboxes.
Author: Cameron Freck | Website URL: |
Comment:
G day great example, just wondering is it possible to use multi-dimensional arrays with this?

Thanks
Re: Admin
PHP does support multi-demensional arrays. However, I have always found that they come with a serious performance hit.