Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable "square bracket to array" parsing of form value names

Tags:

forms

php

As you know, if you have a form containing

<select name="user">
    <option value="a">a</option>
    <option value="b">b</option>
</select>
<input type="text" name="user[name]" />
<input type="password" name="user[password]" />

And you submit that form, in PHP, $_REQUEST['user'] will automatically become an array containing the keys name and password, and the value of the select will be gone. The opposite occurs if the <select> is moved after the other two fields. $_REQUEST['user'] will contain the value of the select, and the values in the inputs will be gone, with nothing being set to either $_REQUEST['user[name]'] or $_REQUEST['user[password]'].

To my knowledge, the same applies to $_POST, $_GET and $_FILES.

Also, input streams are unavailable when the form's MIME is multipart/form-data.

So, is there any way to disable this automatic parsing?

like image 585
Silviu-Marian Avatar asked Nov 01 '25 08:11

Silviu-Marian


1 Answers

A. change

<select name="user">

to

<select name="user[user]">

That way you have a key set for the user array. If you are going to use post arrays you have to make sure every single one has a key.

B. (the way i'd do it)

Forget about the arrays. Use a unique name for each one. Using the arrays give you no benefit. Call the first one user, the second one, user_name, and the 3rd "password"

like image 175
danjfoley Avatar answered Nov 03 '25 23:11

danjfoley