【发布时间】:2017-11-06 19:48:44
【问题描述】:
我有一个表单,用户可以添加额外的行(使用 javaScript),但是当单击保存按钮时,如何获取新数据以分配给变量?
现在发生的情况:用户添加新行,点击保存,这些行与数据一起从页面中消失。
这是我的html:
<table id="desc_table" class="form">
<tbody id="desc_tbody">
<tr id="headings">
<td><font><h3>Description</h3></font></td>
<td><font><h3>Hours</h3></font></td>
<td><font><h3>Rate</h3></font></td>
<td><font><h3>Amount</h3></font></td>
<td></td>
</tr>
<tr>
<td><input name="desc1" type="text" value="<?php echo $desc1; ?>"></td>
<td> <input name="desc_hr1" type="text" value="<?php echo $desc_hr1; ?>"></td>
<td> <input name="desc_rt1" type="text" value="<?php echo $desc_rt1; ?>"></td>
<td><input name="desc_amt1" type="text" value="<?php echo $desc_amt1; ?>"></td>
<td><input type="button" value="X" name="delete" class="btnDelete" onclick="deleteLine(this)"></td>
</tr>
<tr>
<td><input name="desc2" type="text" value="<?php echo $desc2; ?>"></td>
<td> <input name="desc_hr2" type="text" value="<?php echo $desc_hr2; ?>"></td>
<td> <input name="desc_rt2" type="text" value="<?php echo $desc_rt2; ?>"></td>
<td><input name="desc_amt2" type="text" value="<?php echo $desc_amt2; ?>"></td>
<td><input type="button" value="X" name="delete" class="btnDelete" onclick="deleteLine(this)"></td>
</tr>
<tr>
<td><input name="desc3" type="text" value="<?php echo $desc3; ?>"></td>
<td> <input name="desc_hr3" type="text" value="<?php echo $desc_hr3; ?>"></td>
<td> <input name="desc_rt3" type="text" value="<?php echo $desc_rt3; ?>"></td>
<td><input name="desc_amt3" type="text" value="<?php echo $desc_amt3; ?>"></td>
<td><input type="button" value="X" name="delete" class="btnDelete" onclick="deleteLine(this)"></td>
</tr>
</tbody>
</table>
<div>
<center><input type="button" value="Add New Line" id="add_btn" name="add_btn" onclick="newLine()"></center>
</div>
JavaScript:
function newLine(){
//get the total amount of rows in the table
var $rowcount = $('#desc_table tr').length;
//add the row number to the field name to make it unique
var $desc = "desc" + $rowcount;
var $desc_hr = "desc_hr" + $rowcount;
var $desc_rt = "desc_rt" + $rowcount;
var $desc_amt = "desc_amt" + $rowcount;
//create new row data
var $newRowData = '<tr><td><input name=' + $desc + ' type=text></td><td><input name=' + $desc_hr + ' type=text></td><td><input name=' + $desc_rt + ' type=text></td><td><input name=' +$desc_amt+ ' type=text></td><td><input type="button" value="X" name="delete" class="btnDelete" onclick="deleteLine(this)"></td></tr>';
//append new row to bottom of the table
$($newRowData).appendTo($("#desc_table tbody"));
}
PHP 变量声明:
...
if (!empty($_POST['desc1'])){
$desc1 = $_POST['desc1'];
}
else{
$desc1 = NULL;
}
if (!empty($_POST['desc_hr1'])){
$desc_hr1 = $_POST['desc_hr1'];
}
else{
$desc_hr1 = NULL;
}
if (!empty($_POST['desc_rt1'])){
$desc_rt1 = $_POST['desc_rt1'];
}
else{
$desc_rt1 = NULL;
}
if (!empty($_POST['desc_amt1'])){
$desc_amt1 = $_POST['desc_amt1'];
}
else{
$desc_amt1 = NULL;
}
if (!empty($_POST['desc2'])){
$desc2 = $_POST['desc2'];
}
else{
$desc2 = NULL;
}
if (!empty($_POST['desc_hr2'])){
$desc_hr2 = $_POST['desc_hr2'];
}
...
当使用 POST 捕获页面变量时,如何找到新的行变量来声明它们并捕获数据?
【问题讨论】:
-
我没有看到任何 php.ini 文件。什么是php帖子?你的保存处理程序在哪里?
-
刚刚添加了变量声明。这是你想要的?下面我将把数据添加到一个 mySQL 数据库中。我正在考虑的一个解决方案是将该表的行数放入一个全局变量中。这有意义吗?如果是这样,我该怎么做?在 JavaScript 中还是在 php 中?然后我可以做一个循环来收集变量。
-
可能以某种方式使用动态数组?
-
好的 - 谢谢,我会研究 JSON ......可能是我最好的选择。
标签: javascript php