[英]Many Submit buttons for the same html form
hope you fine and well, 希望你一切都好,
i have the following form: 我有以下形式:
<form class="hidden-print" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tr>
<?php
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * from persons";
$i=0;
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo'<input type="hidden" name="id[]" value="' . $row['id'] . ' ">';
echo '<td>'. $row['Name'] . '</td>';
echo'<input type="hidden" name="name[]" value="' . $row['Name'] . ' ">';
$i++;
}
Database::disconnect();
?>
</tr>
</table>
<input class="btn btn-primary" type="submit" value="Send" >
</form>
let me explain what happening here, my form is PHP_SELF so i post the data to the same page, i created a table inside the form which contains ID's and Names, i loop through the selected data and each row in the table will contain id and name of person, as you see, i defined the inputs name and id as arrays eg. 让我解释一下这里发生了什么,我的表单是PHP_SELF,所以我将数据发布到同一页面,在表单中创建了一个包含ID和名称的表格,我遍历了所选数据,表格中的每一行都包含id和如您所见,人名将输入名和ID定义为数组,例如。 name[] and id[] , and i have a submit button at the end of the form will post the arrays id[] and name[].
name []和id [],并且我在表单末尾有一个提交按钮,将发布数组id []和name []。
now what i want is to put an submit input in each row ! 现在我想要的是在每一行中提交一个提交输入! so when i click the submit button it will post just the id and the name of the person in this row !
因此,当我单击“提交”按钮时,它将仅在此行中张贴人员的ID和姓名!
is this possible ?! 这可能吗 ?!
regards. 问候。
You are already using associative array in names eg name[]
您已经在名称中使用关联数组,例如
name[]
Use counters: 使用计数器:
name[<?php echo $i;?>]
And add submit buttons with the same 并添加具有相同的提交按钮
<input type="submit" name="submit[<?php echo $i;?>]"/>
In PHP, loop over $_POST
. 在PHP中,循环
$_POST
。
And check index of submit button using foreach
loop. 并使用
foreach
循环检查提交按钮的索引。
Use the same index for name
, id
, etc. 对
name
, id
等使用相同的索引。
Hope it helps. 希望能帮助到你。
Answer updated with code: 答案已更新为代码:
<form class="hidden-print" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
</tr>
</thead>
<tr>
<?php
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * from persons";
$i=0;
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['id'] . '</td>';
echo'<input type="hidden" name="id['.$row['id'].'" value="' . $row['id'] . ' ">';
echo '<td>'. $row['Name'] . '</td>';
echo'<input type="hidden" name="name['.$row['id'].'" value="' . $row['Name'] . ' ">';
?>
<td>
<input class="btn btn-primary" type="submit" value="Send" name="send<?php echo $rowp['id'];?>" >
</td>
</tr>
<?php
++$i;
}
Database::disconnect();
?>
</tr>
</table>
</form>
<?php
if (! empty($_POST['send'])) {
foreach ($_POST['send'] as $key => $posted) {
$id = isset($_POST['id'][$key]) ? $_POST['id'][$key] : '';
$name = isset($_POST['name'][$key]) ? $_POST['name'][$key] : '';
// Do your SQL here.
}
}
?>
Note: HTML mark ups are used as it is, please correct it as per your needs.
注意:HTML标记按原样使用,请根据需要进行更正。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.