PHP $_POST Variable with brackets

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?
LVL 8
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rgranlundAuthor 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 ManagerCommented:
name = htmlspecialchars($_POST['ii_pi_name’])

This is actually the correct way I was having brain drain moment on my previous post
rgranlundAuthor 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
skullnobrainsCommented:
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
Julian HansenCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.