【问题标题】:How to get Data from table in twig by checking a checkbox如何通过选中复选框从树枝中的表中获取数据
【发布时间】:2016-05-13 00:00:01
【问题描述】:

我在树枝模板中创建了一个表格。在第一列中,您可以选中一个复选框。检查复选框后,它应该删除该行。我的问题是如何让数据删除它。

这是我的桌子

<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['??????'];我不知道我必须在此处放置哪个参数才能从选定的复选框中获取数据。

【问题讨论】:

  • 我建议您在 Ajax 中执行此操作。尝试将您的 PHP 代码放入您通过 Javascript 使用 Ajax 调用的文件中。

标签: php mysql symfony checkbox twig


【解决方案1】:

将复选框的值从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'];

【讨论】:

  • 谢谢。能写个例子吗?
  • 当然,您必须将表格放在指向相应操作的表单标签内,并放置一个“删除选定产品”按钮以提交它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-23
  • 1970-01-01
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
相关资源
最近更新 更多