POSTing Form Fields with same Name Attribute
No. Only the last input element will be available.
If you want multiple inputs with the same name use name="foo[]"
for the input name attribute. $_POST
will then contain an array for foo with all values from the input elements.
<form method="post">
<input name="a[]" value="foo"/>
<input name="a[]" value="bar"/>
<input name="a[]" value="baz"/>
<input type="submit" />
</form>
See the HTML reference at Sitepoint.
The reason why $_POST
will only contain the last value if you don't use []
is because PHP will basically just explode and foreach over the raw query string to populate $_POST
. When it encounters a name/value pair that already exists, it will overwrite the previous one.
However, you can still access the raw query string like this:
$rawQueryString = file_get_contents('php://input'))
Assuming you have a form like this:
<form method="post">
<input type="hidden" name="a" value="foo"/>
<input type="hidden" name="a" value="bar"/>
<input type="hidden" name="a" value="baz"/>
<input type="submit" />
</form>
the $rawQueryString
will then contain a=foo&a=bar&a=baz
.
You can then use your own logic to parse this into an array. A naive approach would be
$post = array();
foreach (explode('&', file_get_contents('php://input')) as $keyValuePair) {
list($key, $value) = explode('=', $keyValuePair);
$post[$key][] = $value;
}
which would then give you an array of arrays for each name in the query string.
Multiple form fields with same 'name' attribute not posting
I've worked out a brute-force solution. Note that I'm pretty aware this is a hack. But I'm stuck in the position of having to work around other code that I have no control over.
Basically, I've created an ONSUBMIT handler which examines the form for the repeated hidden fields and makes sure they are all populated with the correct data. This seems to guarantee that the POST string contains data regardless of how the form gets rendered and the Java back end appears to be happy with it as well.
I've tested this in the following situations:
- Code generates single instances of the hidden fields (which does happen sometimes)
- Code generates multiple instances of the hidden fields
- Code generates no instances of the hidden fields (which should never happen, but hey...)
My 'else' condition contains a tiny bit of MooTools magic, but it's otherwise straight-forward stuff.
Maybe someone else will find this useful one day...
Thanks for the help!
<form method="post" name="loginform" id="loginform" action="/login" onsubmit="buildDeviceFP(this);">
<script type="text/javascript">
function insertFieldValues( fields, sValue )
{
if ( 'length' in fields )
{
// We got a collection of form fields
for ( var x = 0; x < fields.length; x++ ) {
fields[x].value = sValue;
}
}
else
{
// We got a single form field
fields.value = sValue;
}
}
function buildDeviceFP( oForm )
{
// Get the element collections for Device Fingerprint & Language input fields from the form.
var devicePrintElmts = oForm.elements.deviceprint;
var languageElmts = oForm.elements.language;
// 'devicePrintElmts' & 'languageElmts' *should* always exist. But just in case they don't...
if ( devicePrintElmts) {
insertFieldValues( devicePrintElmts, getFingerprint() );
} else if ( oForm.deviceprint ) {
oForm.deviceprint.value = getFingerprint();
} else {
$('logonbox').adopt(
new Element( 'input', {'type':'hidden', 'name':'deviceprint', 'value':getFingerprint()} )
);
}
if ( languageElmts) {
insertFieldValues( languageElmts, getLanguage() );
} else if ( oForm.language ) {
oForm.language.value = getLanguage();
} else {
$('logonbox').adopt(
new Element( 'input', {'type':'hidden', 'name':'language', 'value':getLanguage()} )
);
}
}
</script>
Multiple Forms With Input Fields With The Same Name Attribute? Good Or Bad?
Agree with above answer. Name is totally ok, and will be passed as response parameter of your form. Different story would be if your input elements would have same id's as well - some browsers might have problems traversing dom of your document.
Again, think of bunch of radio buttons, where users can select gender etc. They must have same name (but different id's)...
Trying to post a form with multiple fields with same name as an array
Sure you can, simply use an empty [], everytime this one is encountered, it is considered as a new sibling
<tr>
<td><label>Name:</label></td>
<td><input type="text" name="appsDetails[][name]" placeholder="Enter name" value=""/></td>
<td><label>ID:</label></td>
<td><input type="text" name="appsDetails[][id]" placeholder="Enter id" value=""/></td>
<td><label>Token:</label></td>
<td><input type="text" name="appsDetails[][token]" placeholder="Enter token" value=""/></td>
</tr>
POST multiple fields with same name in Laravel
thanks all. this code worked for me. i used for loop with a index
<input type="hidden" name="rowid[]" value="{{$datacart->rowid}}">
<input type="text" name="quantity[]" value="{{$datacart->quantity}}">
,
for ($i = 0; $i < count($request->input('rowid')); $i++){
Cart::update($request->rowid[$i], $request->quantity[$i]);
}
How do I post multiple inputs with same name and different values
$products = $_POST['product'];
$orderedItems = "Ordered items:";foreach($products as $product) { $orderedItems .= "$product ";}
mail("your@email.com", "Subject line", $orderedItems);
Related Topics
How to Overwrite a Function in PHP
Mixing a PHP Variable With a String Literal
How to Validate Array in Laravel
Pdo With Insert into Through Prepared Statements
: Operator (The 'Elvis Operator') in PHP
Amazon S3 - How to Fix 'The Request Signature We Calculated Does Not Match the Signature' Error
Is Native PHP Support For Web Sockets Available
PHP List All Files in Directory
Convert Number to Month Name in PHP
How to Replace Special Characters With the Ones They'Re Based on in PHP
How to Create a Copy of an Object in PHP
Does PHP Time() Return a Gmt/Utc Timestamp
Which $_Server Variables Are Safe