html表中表单数据的处理
我有一个动态表,数据库中的每一行都有html表中表单数据的处理,html,ajax,forms,Html,Ajax,Forms,我有一个动态表,数据库中的每一行都有select和input type=“text”。如何设置attr“name”,以便在服务器端正确读取选择和输入,以及如何使用ajax传递值?所有名称都应该是动态的、唯一的还是带有数组的……表旁边的表单中有更多的输入,但我只关心表数据。表是可排序的,所以行会改变位置,序列化也会改变顺序 <table id="config" class="table" cellspacing="0" width="100%"> <thead>
select
和input type=“text”
。如何设置attr“name”
,以便在服务器端正确读取选择和输入,以及如何使用ajax传递值?所有名称都应该是动态的、唯一的还是带有数组的……表旁边的表单中有更多的输入,但我只关心表数据。表是可排序的,所以行会改变位置,序列化也会改变顺序
<table id="config" class="table" cellspacing="0" width="100%">
<thead>
<tr>
<th>name</th>
<th>Action</th>
<th class="no-sort">tag</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name 1</td>
<td>
<div class="form-group">
<select class="full-width config-action" name="action" data-init-plugin="select2" data-disable-search="true">
<option value="1">1</option>
<option value="2" selected>2</option>
<option value="3">3</option>
<option value="tag">Tag</option>
</select>
</div>
</td>
<td>
<input type="text" name="tag" class="form-control table-tag" >
</td>
</tr>
<tr>
<td>Name 2</td>
<td>
<div class="form-group">
<select class="full-width config-action" name="action" data-init-plugin="select2" data-disable-search="true">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="tag">Tag</option>
</select>
</div>
</td>
<td>
<input type="text" name="tag" class="form-control table-tag" >
</td>
</tr>
</tbody>
</table>
在提交表单之前,您可以通过以下方式动态添加“名称”属性:
$("input.table-tag").each(function(i){
$(this).attr("name","tag"+i);
})
名称将变为tag0、tag1等。不确定这是否是必需的。表单元素应在HTML中正确命名 例如:
<select name="table[1][action]" ...
<input name=table[1][tag]" ...
...
<select name="table[2][action]" ...
<input name=table[2][tag]" ...
$key
变量通常是数据库ID
<select name="table[1][action]" ...
<input name=table[1][tag]" ...
...
<select name="table[2][action]" ...
<input name=table[2][tag]" ...
foreach($_POST['table'] as $key => $row){
// now use $key, $row['tag'], $row['action'] ...
}