如何从中获取值并在php中保存到数据库
我有一个带有复选框的表,我想编辑一些td,并想得到值。 这是我的密码如何从中获取值并在php中保存到数据库,php,html,symfony,twig,Php,Html,Symfony,Twig,我有一个带有复选框的表,我想编辑一些td,并想得到值。 这是我的密码 <table class="table table-bordered table-striped table-hover"> <thead> <tr> <th><input type="checkbox" id="allcb" name="allcb"/> Check all</th>
<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>
另一个选项是设置复选框值,数据由某个值分隔
<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>
您是在问如何提交表单吗?我想从{{user.lastname}}获取新值。谢谢。我得到的是上一个值我没有得到编辑值:/Edited。在提交表单之前,您需要用新数据填充隐藏字段。
$("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>