【发布时间】:2020-03-30 14:49:32
【问题描述】:
我正在尝试制作动态表格,允许人们将多个牙齿数据添加到数据库中。我遇到的问题是,在提交表单后,我无法访问给定的变量和数组(复选框)只有 1 个值(第一个)。
为了生成表单,我使用 jQuery,关于牙齿的数据是复选框。
生成表单动态部分的代码:
<script type="text/javascript">
function add_row()
{
$rowno=$("#teeth_table tr").length;
$rowno=$rowno+1;
$("#teeth_table tr:last").after("<tr id='row"+$rowno+"'>" +
"<td style='margin-left: 15px'> Quarter <input type='number' name='quarter[]' min='1' max='4' placeholder='1-4'></td>" +
"<td style='margin-left: 15px'> Tooth number <input type='number' name='number[]' min='1' max='8' placeholder='1-8'></td>" +
"<td style='margin-left: 15px'> Description <input type='text' name='desc[]'></td>" +
"<td style='margin-left: 15px'> Measurement <input type='text' name='measure[]'></td>" +
"<td style='margin-left: 15px'><input type='button' value='DELETE' onclick=delete_row('row"+$rowno+"')></td></tr>");
}
function delete_row(rowno)
{
$('#'+rowno).remove();
}
</script>
表格代码:
echo("
<div style='margin-left: 30px'>
<hr>
<h4>Procedure data</h4>
<div id='form_div'>
<div style='margin-left: 20px'>
<form method='post' action='dental_chart.php'>
<p>VAT_client:
<input type='text' name='VAT_client' value='$VAT_client' disabled/></input>
</p><p>VAT_doctor:
<input type='text' name='VAT_doctor' value='$VAT_doctor' disabled/></input>
</p><p>Date:
<input type='date' name='date' value='$date' disabled/></input>
</p><p>Procedure type:
<input type='text' name='type' value='$procedure' disabled/></input>
</p><p>Description:
<input type='text' name='description' placeholder='describe procedure'/>
</p>
</div>
");
?>
<hr>
<h4>Insert procedure charting data - gap per tooth</h4>
<div style='margin-left: 20px'>
<table id="teeth_table">
<tr id="row1">
<td style="margin-left: 15px"> Quarter <input type='number' name='quarter[]' min="1" max="4"
placeholder="1-4"></td>
<td style="margin-left: 15px"> Tooth number <input type='number' name='number[]' min="1" max="8"
placeholder="1-8"></td>
<td style="margin-left: 15px"> Description <input type='text' name='desc[]'></td>
<td style="margin-left: 15px"> Measurement <input type='text' name='measure[]'></td>
</tr>
</table>
<input type="button" onclick="add_row();" value="ADD TOOTH">
</div>
<p></p>
<hr>
<div style="text-align: center">
<input type="submit" name="submit_row" value="SUBMIT FORM">
</div>
</form>
如果 POST 数据不为空,我的 php 部分将访问它:
if (!empty($_POST)) {
$VAT_client_p = $_POST['VAT_client'];
$VAT_doctor_p = $_POST['VAT_doctor'];
$date_p = $_POST['date'];
$type = $_POST['type'];
$description = $_POST['description'];
# dental chart multiple data
$quarter = $_POST['quarter'];
$number = $_POST['number'];
$desc_tooth = $_POST['desc'];
$measure = $_POST['measure'];
echo("<p>VAT_client = ". $VAT_client_p .", VAT_doctor = ". $VAT_doctor_p.", date: ".$date_p ."</p>");
for($i=0;$i<count($_POST['quarter']);$i++)
{
if($quarter[$i]!="" && $number[$i]!="" )
{
echo("<p>". count($quarter)." </p>");
echo("<p>". count($desc_tooth)." </p>");
echo("<p>quarter = '$quarter[$i]', tooth number = '$number[$i]', tooth desc: '$desc_tooth[$i]'</p>");
}
}
}
此时我只想在提交后访问变量并回显它们,当这部分工作时,我将执行事务以将数据插入数据库。我将不胜感激任何建议或建议,我被困在这一点上。
(运行form first image的结果是数组只有一个元素,VAT_doctor、VAT_client等都是空的)
【问题讨论】:
-
我刚试过你的代码。对我来说,它也在打印所有添加的行。
-
VAT_client 和 VAT_doctor 为空,因为我无法在 HTML 中设置它们。它们被禁用并且没有任何价值。数组 POST 季度可以在 for 循环中访问,并且为我工作
-
@davidev 你是如何尝试这段代码的?你能简单地给我解释一下吗?
-
你得到什么错误?我用你的代码在我的服务器上运行它。之后我可以发送表单并获取输出。您打印所有输入
-
进入页面后我分配的那些值,我通过上一页的GET方法提供它们。我没有发现任何错误,我现在会检查它(对不起,这是我在 PHP 中的第一个项目)
标签: php jquery forms post checkbox