如何从获取值并保存到php的数据库
问题描述:
我有一个带有复选框的表格,我想编辑一些td并想获取值。 这里是我的代码如何从获取值并保存到php的数据库
<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'];但我没有得到任何东西。
有人可以帮助我吗?谢谢!
答
您至少有两个选项:
隐藏字段无处不在,如果数据(最好的方法):
<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 ...
}
Anothe选择是设置复选框值与数据分隔的某个值(丑陋的解决方案)
<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>
+0
谢谢。我得到以前的值我没有得到编辑值:/ –
+0
编辑。您需要在提交表单之前用新数据填充隐藏字段。 –
您是否在问如何提交表单? –
我想从