我有这种形式,用户可以在其中添加多行取决于他/她的偏好。使用jquery生成每个输入标记的名称; meal1 ... amount1,meal2 ... amount2等等。
我的问题是如何将所有这些值插入我的MySQL数据库?
到目前为止,我有这个功能:
function dbRowInsert($table_name, $form_data)
{
// retrieve the keys of the array (column titles)
$fields = array_keys($form_data);
// build the query
$sql = "INSERT INTO ".$table_name."
(`".implode('`,`', $fields)."`)
VALUES('".implode("','", $form_data)."')";
// run and return the query result resource
return mysql_query($sql);
}
HTML:
<div id ="ca_meals"><!--start of ca_meals-->
<div class="container">
<div class="row clearfix">
<div class="col-md-12 column">
<h3>Meals Form</h3>
<div class="form-group">
<label for="ca_meals_date">Date:</label>
<input type="text" class="form-control" id="ca_meals_date" name="ca_meals_date" placeholder="Date">
</div>
<div class="form-group">
<label for="ca_meals">Purpose:</label>
<input type="text" class="form-control" id="ca_meals_purpose" placeholder="Purpose">
</div>
<table class="table table-bordered table-hover" id="tab_logic_meals">
<thead>
<tr >
<th class="text-center">
Meals
</th>
<th class="text-center">
Number of PAX
</th>
<th class="text-center">
Alloted Budget Per PAX
</th>
<th class="text-center">
Amount
</th>
</tr>
</thead>
<tbody>
<tr id='meals0'>
<td>
<input type="text" name='ca_accommodate' placeholder='Meals' class="form-control"/>
</td>
<td>
<input type="text" name='ca_meals_numberpax' placeholder='Number of PAX' class="form-control"/>
</td>
<td>
<input type="text" name='ca_meals_budgetpax' placeholder='Alloted Budget Per PAX' class="form-control"/>
</td>
<td>
<input type="text" name='ca_meals_amount' placeholder='Amount' class="form-control"/>
</td>
</tr>
<tr id='meals1'></tr>
</tbody>
</table>
<div class="form-group">
<label for="ca_total">Total:</label>
<input type="text" class="form-control" id="ca_total" disabled>
</div>
</div>
</div>
<a id="add_row_meals" class="btn btn-primary pull-left">Add Row</a><a id='delete_row_meals' class="pull-right btn btn-danger">Delete Row</a>
</div>
</div><!--end of ca_meals-->
这是我的JQuery:
$("#add_row_meals").click(function(){
$('#meals'+l).html("<td><input name='ca_meal"+l+"' type='text' placeholder='Meals' class='form-control input-md' /> </td><td><input name='ca_meals_numberpax"+l+"' type='text' placeholder='Number of PAX' class='form-control input-md'></td><td><input name='ca_meals_budgetpax"+l+"' type='text' placeholder='Alloted Budget Per PAX' class='form-control input-md'></td><td><input name='ca_meals_amount"+l+"' type='text' placeholder='Amount' class='form-control input-md'></td>");
$('#tab_logic_meals').append('<tr id="meals'+(l+1)+'"></tr>');
l++;
});
$("#delete_row_meals").click(function(){
if(l>1){
$("#meals"+(l-1)).html('');
l--;
}
});
答案 0 :(得分:0)
通常通过使用字段名称传递数组或索引来完成。
比如,在为名称添加DHTML字段时,在JavaScript中,将其称为meals[]
在PHP中,$_POST['meals']
将包含一个数组
另一种方式 - 索引。比如,当您使用Javascript创建新字段时,请为其指定一个新名称,如meals_1
,meals_2
等。然后在PHP中循环它们。
对于第一种情况,使用meals[]
,POST请求将如下:
&meals[]=aaa&meals[]=bbb&meals[]=ccc&amount[]=1&amount[]=2&amount[]=3
使用meals[]
的PHP代码示例如下:
for($i=0;$i<count($_POST['meals']);$i++)
{
$sql = "INSERT INTO ... SET
`meals` = ". $_POST['meals'][$i].",
`amount` = ". $_POST['amount'][$i].",
// etc
}