Multiple inputs with same name through POST in php
Change the names of your inputs:
<input name="xyz[]" value="Lorem" />
<input name="xyz[]" value="ipsum" />
<input name="xyz[]" value="dolor" />
<input name="xyz[]" value="sit" />
<input name="xyz[]" value="amet" />
Then:
$_POST['xyz'][0] == 'Lorem'
$_POST['xyz'][4] == 'amet'
If so, that would make my life ten times easier, as I could send an
indefinite amount of information through a form and get it processed
by the server simply by looping through the array of items with the
name "xyz".
Note that this is probably the wrong solution. Obviously, it depends on the data you are sending.
Form send data to php with multiple inputs with the same name
There are different possibilities how to do that. One Suggestion (i limit it to the parts which are relevant)
form (note that i gave work_name
the id as an index, use_it
not (but it could have)
<td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
<input type="hidden" name="work_name[<?php echo $work->id?>]" value="<?php echo $work->name; ?>" />
The form only submits the checked checkboxes values in an array, all other are omited. Therefore we could loop over the checked checkbox values like this
foreach($_POST['useit'] as $work_id){
$work_name = $work_names[$work_id];
//do something with the checked rows only (all others are not looped over)
}
This is only possible, due to the given id as an array key in the form work_name[<?php echo $work->id?>]
.
A general sidenote for better (and more secure) code: please note that your data could be modified by the user, and send back with wrong data (or worse). So please make sure to sanitize your input, or probably better in this case only submit the id in question and the new data and pickup the rest directly from your database. So you can make sure the hidden data has not been modified on the client side and send back wrong.
multiple inputs of the same name in an html form
If there are duplicated names then the values will be in an array with the name. The demo below sends to a live test server and the response is directed to an <iframe>
<form action="https://httpbin.org/post" method="post" target="response"> <input name="name" value="val"> <input name="name" value="val"> <input type="submit"></form>
<iframe name='response'></iframe>
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 Get the Last Inserted Id of a MySQL Table in PHP
Sort an Array by Keys Based on Another Array
How to Get Parameters from a Url String
MySQL and Nosql: Help Me to Choose the Right One
Among $_Request, $_Get and $_Post Which One Is the Fastest
Why Can't I Access Datetime-≫Date in PHP'S Datetime Class
Fatal Error: Uncaught Error: Call to Undefined Function MySQL_Connect()
Suppress Error With @ Operator in PHP
What Are the Best PHP Input Sanitizing Functions
How to Decode Unicode Escape Sequences Like "\U00Ed" to Proper Utf-8 Encoded Characters
How to Convert Json String to Array
How to Post Json Data With PHP Curl
How Can One Check to See If a Remote File Exists Using PHP
PHP: How to Determine Every Nth Iteration of a Loop