我在 Twig 模板中创建了一个表格。在第一列中,您可以选中一个复选框。选中复选框后,它应该删除该行。我的问题是如何获取数据以将其删除。
这是我的 table
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th> <label for="checbox">Check</label></th>
<th>id</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
{% for products in user.products %}
<tr>
<td><input type="checkbox" name="checkBox" id="ic" value=true/></td>
<td >{{ products.id }}</a></td>
<td >{{ products.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
if(isset($_POST['checkBox'])== 'true'){
$products = $_POST['??????'];
$zl = 0;
foreach ($products as $pr)
{
$em = $this->getDoctrine ()->getManager ();
$pr = $em->getRepository('UserBundle:Products')->find($pr);
$user->removeProduct($pr);
++$zl;
}
$em->persist($user);
$em->flush();
}
如何获取数据? $products = $_POST['??????'];
我不知道必须在此处放置哪个参数才能从所选复选框获取数据。
最佳答案
将复选框的值从 true
更改为行的产品 ID,并检查 $_POST['checkBox']
是否为数组且不为空。
替换html部分
<input type="checkbox" name="checkBox" id="ic" value=true/>
与
<input type="checkbox" name="checkBox[]" id="ic" value="{{ products.id }}" />
和php部分
if(isset($_POST['checkBox'])== 'true'){
$products = $_POST['??????'];
与
if(isset($_POST['checkBox']) && !empty($_POST['checkBox'])){
$products = $_POST['checkBox'];
关于php - 如何通过选中复选框从 Twig 中的表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37190862/