我有一个通过javascript添加新表行的脚本,我使用foreach
函数在mysql中插入表行,foreach
函数没有插入使用添加行按钮动态添加的所有行的数据,PHP函数仅插入一个/第一行的表,如<tr>td tags</tr>
中已知的第一行html
,当加载时...
但请注意,当我在html foreach函数中手动添加td标签行时,正确工作并插入所有行.. 我需要帮助才能在js ..
添加dynamicaly的行中插入所有数据这是html:
<form action="index.php" method="post">
<table border="0" cellspacing="0" class="table table-bordered table-hover" data-name="cont-table">
<thead style="">
<tr>
<th class="text-center"> m </th>
<th class="text-center"> item </th>
<th class="text-center"> unit </th>
<th class="text-center"> quantity </th>
<th class="text-center"> unit price </th>
<th class="text-center"> financial years </th>
<th class="text-center"> total</th>
</tr>
</thead>
<tr>
<td><span id="snum">1.</span></td>
<td>
<select class="select" id="first_name" name="items[]">
<option>1</option>
<option>2</option>
</select></td>
<td>
<select class="select" id="last_name" name="units[]">
<option>1</option>
<option>2</option>
</select></td>
<td><input class="select qty" type="number" id="tamil" name="qtys[]"/></td>
<td><input class="select unit" type="number" id="english" name="unitno[]" /></td>
<td>
<select class="select " id="computer" name="financials[]">
<option>1</option>
<option>2</option>
</select></td>
<td><input type="text" id="total" name="totals[]" class="amount" value="" /></td>
</tr>
</table>
<button id="add_row" type="button" class="delete btn btn-default pull-right">delete</button>
<button id="add_row" style="margin-right:5px;" type="button" class="addmore btn btn-default pull-right">add row</button>
<input name="set-cont" type="submit" class="sb-btn center" value="حفظ"/>
</form>
这是添加行的javascript:
$(".addmore").on('click',function(){
count=$('table tr').length;
var data="<tr><td><span id='snum"+i+"'>"+count+".</span></td>";
data +="<td><select class='select' id='first_name' name='item[]'><option>1</option><option>2</option></select></td> <td><select class='select' id='last_name' name='unit[]'><option>1</option><option>2</option></select></td><td><input type='text' class='select qty' id='tamil' name='qty[]'/></td><td><input type='text' class='select unit' id='english' name='unitno[]'/></td><td><select class='select' id='computer' name='financial[]'><option>1</option><option>2</option></select></td><td><input type='text' class='amount' id='total' name='total[]'/></td></tr>";
$('table').append(data);
i++;
});
这是将数据插入mysql的PHP代码:
if(isset($_POST['set-cont'])) {
foreach ($_POST['items'] as $key => $value) {
$items = $_POST["items"][$key];
$units = $_POST["units"][$key];
$qtys = $_POST["qtys"][$key];
$unitno = $_POST["unitno"][$key];
$financials = $_POST["financials"][$key];
$totals = $_POST["totals"][$key];
try {
$db = DB();
$query = $db->prepare("INSERT INTO table_contract (items, units, qtys, unitno, financials, totals) VALUES(:items,:units,:qtys,:unitno,:financials,:totals)");
$query->bindParam("items", $items, PDO::PARAM_STR);
$query->bindParam("units", $units, PDO::PARAM_STR);
$query->bindParam("qtys", $qtys, PDO::PARAM_STR);
$query->bindParam("unitno", $unitno, PDO::PARAM_STR);
$query->bindParam("financials", $financials, PDO::PARAM_STR);
$query->bindParam("totals", $totals, PDO::PARAM_STR);
$query->execute();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}
答案 0 :(得分:0)