Get the values of 2 HTML input tags having the same name using PHP
When you have repeated names, you have to give them array-style names:
<form action="bla.php" method=post>
<table class="pv-data">
<tr>
<td><input type="text" name="id[]" size="2" value=1 /></td>
<td><input type="text" name="longitude[]" size="7"/></td>
<td><input type="text" name="latitude[]" size="7"/></td>
</tr>
<tr>
<td><input type="text" name="id[]" size="2" value=2 /></td>
<td><input type="text" name="longitude[]" size="7"/></td>
<td><input type="text" name="latitude[]" size="7"/></td>
</tr>
</table>
<input type="submit" name="submit" value="SUBMIT">
</form>
When you do this, $_POST['id']
, $_POST['latitude']
, and $_POST['longitude']
will be arrays containing the values.
Your form processing code can then iterate over these:
for ($i = 0; $i < count($_POST['id']); $i++) {
if (isset($_POST['latitude'][$i], $_POST['longitude'][$i])) { // Make sure both are filled in
// Do stuff with this row of the form
}
}
how to get value of Multiple text inputs with same name
$name = $_POST['name'];
$age = $_POST['age'];
for($l=0; $l < count($name); $l++)
{ //your condition
}
don't forget to add this in your html
<label>Name</label>
<input type="text" size="20" name="name[]" id="name" >
</td>
<td>
<label>Age</label>
<input type="text" size="20" name="age[]" id="age" >
</td>
PHP How to send multiple values within the same name
Send inputs as array answer
. Then you will have these values in array variable $_POST['answer']
with corresponding IDs or you can send without IDs and then type name="answer[]"
.
<form action="addsurvey.php" method="post">
<input type="text" name="surveyname">
<input type="text" name="question" placeholder="First Question">
<br>
<input type="text" name="answer[1]" value="">
<input type="text" name="answer[2]" value="">
<input type="text" name="answer[3]" value="">
<br>
<input type="submit" name="submit">
</form>
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.
Related Topics
Which Is Faster? Constants, Variables or Variable Arrays
Using Windows Authentication with PHP
PHP Include File in Webroot from File Outside Webroot
Illegal Command Error Code 127 in PHP Exec Function
Sort a Set of Multidimensional Arrays by Array Elements
What Is the Name for the "<<<" Operator
Mail(): Smtp Server Response: 550 the Address Is Not Valid Error on Hmailserver
Laravel Middleware with Multiple Roles
Is There Java Hashmap Equivalent in PHP
What Is the Use of <<<Eod in PHP
Create Programmatically a Product Using Crud Methods in Woocommerce 3