11 Vote

Send form input as an array to PHP script

Tip by Stefan Trost | 2012-10-29 at 16:31

It is often quite handy to receive inputs from a form directly as an array in a PHP script to be able to directly start processing the data in the correct form. For example, at a form that has many similar fields. In this tip, I will show you, how to do this.

A first example

In our first example, we have three input fields and we want to receive their content in our PHP script "script.php" as an array.

<form action="script.php" method="post">
  <input name="field[]" value="" />
  <input name="field[]" value="" />
  <input name="field[]" value="" />
</form>

For this, we name each input field with the name "field[]". "field" is an arbitrary name, the square brackets indicate that we want to transfer the data as an array.

// receive all data in an array
$fields = $_POST['field'];
  
// output or process all data
foreach ($fields as $value) {
  echo $value;
}

In our PHP script, we can use $_POST['field'] to receive the array and to directly process the data. Here, we save the work of having to address and read each field individually.

Names of the fields in the array

Alternatively, we can give the individual fields in the array a name:

<form action="script.php" method="post">
  <input name="feld[firstname]" value="" />
  <input name="feld[familyname]" value="" />
  <input name="feld[password]" value="" />
</form>

Accordingly, we can receive the data then in our PHP script:

// receive all data in an array
$fields = $_POST['field'];
 
// read out fields by names
$firstname  = $fields['firstname'];
$familyname = $fields['familyname'];
$password   = $fields['password'];

This makes it easier to keep the individual fields apart.

More possibilities

Of course you can also transfer any number of arrays next to each other and combine them with non-array fields:

<form action="script.php" method="post">
  <input name="field[]" value="" />
  <input name="field[]" value="" />
  <input name="otherarray[]" value="" />
  <input name="normalfield" value="" />
  <input name="otherarray[]" value="" />
</form>

And read out in PHP:

$field = $_POST['field'];
$otherarray = $_POST['otherarray'];
$normalfield = $_POST['normalfield'];
 
foreach ($field as $value) echo $value;
foreach ($otherarray as $value) echo $value;
echo $normalfield;

With this, you can define already in the form, in which form the data should be available later.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.