PHP $_POST Variable with brackets

Robert Granlund
Robert Granlund used Ask the Experts™
on
PHP the correct way to create a form variable to insert into a DB

my form has this:

<html>
  <textarea name="ii_pi_name[]" value="<?= $ii_pi_name1 ?>" ></textarea>

Open in new window


My PHP has this but it is not working.
<?php
$ii_pi_name = $_POST['ii_pi_name'][''];

Open in new window

How do I write that so it works with the bracket?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
@ron, can you please show me that written out in the format I have?  I'm a little over my head right now.
Ron MalmsteadInformation Services Manager

Commented:
name = htmlspecialchars($_POST['ii_pi_name’])

This is actually the correct way I was having brain drain moment on my previous post

Author

Commented:
@ron, so you are saying this:

$ii_pi_name =  htmlspecialchars($_POST['ii_pi_name']);  When an array is being passed? Because that does not work to insert that var into the DB
array_pop($_POST['ii_pi_name']) will give you the first elt of the array

the array has no keys so php indexes it numerically, so the first elt is actually $ii_pi_name = $_POST['ii_pi_name'][0];

when in doubt, just print the whole thing with var_export or var_dump or even print_r
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Using array notation in forms with a PHP backend

<textarea name="ii_pi_name[]" value="<?= $ii_pi_name1 ?>" ></textarea>

Open in new window

While array notation is useful to process forms - you need to use it in the right way. The above is useful if you have many textarea controls that you need to group together.

If you have just one then an open array is not really that useful.
It would be better to do something like this
<textarea name="data[pi_name]" value="<?= $ii_pi_name1 ?>" ></textarea>

Open in new window

What we are doing here is using data as the container for the form data and then using the index name as the field.

In this way we can do this
$data = isset($__POST['data']) ? $_POST['data'] : false;
if ($data) {
   echo "Textarea value = {$data['pi_name']}";
}

Open in new window


Going back to your implementation you could do this
$data = isset($__POST['data']) ? $_POST['data'] : false;

Open in new window

Same as before except now your data is available only through its numeric index in the array. If we dump the above (assuming no other fields) we get this
Array (
   0 => 'Value from textarea'
)

Open in new window

This is not that useful - because if you add other elements before the text area for instance if you add this to the front of your form
<input type="text" name="ii_pi_name[]">

Open in new window

Now the dump of $data gives you
Array (
  0 => 'input value',
  1 => 'textarea value'
)

Open in new window


It is difficult to make a proper recommendation but on the balance I would personally go with the named index for the array data.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial