【问题标题】:How to get the value from<td> and save to Database in php如何从 <td> 获取值并保存到 php 中的数据库
【发布时间】:2016-09-12 18:46:13
【问题描述】:

我有一个带有复选框的表格,我想编辑一些 td 并想要获取值。 这是我的代码

<table class="table table-bordered table-striped table-hover">
    <thead>
        <tr>
            <th><input type="checkbox" id="allcb" name="allcb"/> Check all</th>
            <th data-field="id">ID</th>
            <th data-field="firstName">First name</th>
            <th data-field="lastName">Last name</th>
        </tr>
    </thead>
    <tbody>
        {% for user in product.users %}
                <td><input type="checkbox" name="checkBox[]"  id="ic" value="{{ user.id }}" /></td>
                <td  data-title="id">{{user.id}}</td>
                <td data-title="firstName">{{user.firstname}}</td>
                <td data-title="lastName" contenteditable='true' name="v1">{{user.lastname}}</td>

            </tr>
        {% endfor %}
    </tbody>

</table>
if(isset($_POST['checkBox']) && !empty($_POST['checkBox'])&& isset($_POST['v1'])){

         $user =$_POST['checkBox'];
        $v1 =$_POST['v1'];

         $zl = 0;
         foreach ($user  as $id)
         {
           foreach ($v1 as $v2)
           {

             $userd = $em->getRepository('UserBundle:User')->find($id);
             $userd->setlastName($v2);
             $em->persist($userd);


             ++$zl;
           }}

        $em->flush();
      }

我没有得到任何结果。我试图获得这样的价值: $v1 =$_POST['v1'];但是我什么都没有。

有人可以帮助我吗?谢谢!

【问题讨论】:

  • 您是在问如何提交表单吗?
  • 我想从 {{user.lastname}}

标签: php html symfony twig


【解决方案1】:

您至少有两个选择:

如果有数据,则到处隐藏字段(最佳方法):

<td>
    <input type="checkbox" name="checkBox[]"  id="ic" value="{{ user.id }}" />
    <input type="hidden" value="{{user.lastname}}" name="v1[{{ user.id }}]"></td>
<td  data-title="id">{{user.id}}</td>
<td data-title="firstName">{{user.firstname}}</td>
<td data-title="lastName" contenteditable='true'>{{user.lastname}}</td>

在服务器端你可以这样做:

.... your code ....
foreach ($user  as $id) {
    $lastname = $v1[$id];
    ... save your lastname value ...
}

另一个选项是设置复选框值,数据由某个值分隔(丑陋的解决方案)

<td><input type="checkbox" name="checkBox[]"  id="ic" value="{{ user.id }}#{{user.lastname}}" /></td>
            <td  data-title="id">{{user.id}}</td>
            <td data-title="firstName">{{user.firstname}}</td>
            <td data-title="lastName" contenteditable='true' name="v1">{{user.lastname}}</td>

编辑

我现在看到您正在使用 contenteditable。您应该将值添加到隐藏字段:

$("form").submit(function() {
    $('input[name*="v1"]').val($(this).closest('td[name="v1"]').html());
});

<td>
    <input type="checkbox" name="checkBox[]"  id="ic" value="{{ user.id }}" />
    <input type="hidden" value="{{user.lastname}}" name="v1[{{ user.id }}]"></td>
<td  data-title="id">{{user.id}}</td>
<td data-title="firstName">{{user.firstname}}</td>
<td data-title="lastName" contenteditable='true' name="v1">{{user.lastname}}</td>

【讨论】:

  • 谢谢。我得到以前的值我没有得到编辑值:/
  • 已编辑。在提交表单之前,您需要使用新数据填充隐藏字段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多