Linas
Linas

Reputation: 4408

Use $_POST values by number

i'm not sure how to explain it correctly, but i would like to use $_POST values like this $_POST[0] and not like this $_POST['form_field_name'].

Let's say i have 3 form fields so if i'd like to get data from post like this:

echo $_POST[0]; echo $_POST[1]; echo $_POST[2];

I hope you guys understand what i wanna do here.

Upvotes: 0

Views: 2689

Answers (4)

Galled
Galled

Reputation: 4206

$_POSTs variables depends on the name attribute of the form element as you can read in this link.

On the other hand the attribute name of the form elements according to W3C always must begin with a letter.

But I think you can prepare the $_POST variable before all your code (at the begining of your php script) with:

$arrPostVariables = array_values($_POST);

And then call them in the way as you want, but I think you will previously must detect the order of the array in order to avoid errors by not having the text of each variable.

Upvotes: 2

Alexandre
Alexandre

Reputation: 3170

This should work:

<input name="0" value="val0" />
<input name="1" value="val1" />

Upvotes: -2

Jan Dragsbaek
Jan Dragsbaek

Reputation: 8101

I would not recommend ever to refer to your $_POST values with indexes, as it is generally a bad idea.

You can access them by indexes if you do this:

$items = array_values($_POST);
$foo = $items[0];
$bar = $items[1]

You can also run through your values with a foreach loop, like this (which is better, but still bad!)

foreach($_POST as $item)
{
    // do your thing here
}

Upvotes: 3

Till Helge
Till Helge

Reputation: 9311

Try it like this:

 $values = array_values($_POST);

No idea why you would do such a thing though.

Upvotes: 10

Related Questions