php - 如何通过选中复选框从 Twig 中的表中获取数据

标签 php mysql symfony checkbox twig

我在 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/

相关文章:

php - 将多个 MySQL 查询与 PHP 相关联?

asp.net - MySqlDataAdapter 首次加载时无法检索数据

php - api平台: how secure custom operation

php - MySQL SELECT SUM(Column) 和 SELECT * 违反基数 : 1241 Operand should contain 1 column(s)

php - 如何使用php获取大量数据并将其加载到jquery数据表中

PHP 在连接到 MySql 时忽略 ssl 证书

MySQL删除重复记录但保留最新记录

php - 如何获取 symfony 2.3 中的当前语言环境?

symfony - 如何访问 symfony2 中实体的存储库方法?

php - 在 twig symfony 中显示所有表单错误