php - PHP、动态表单、访问 POST 变量、复选框
问题描述
我正在尝试制作动态表格,允许人们将多个牙齿数据添加到数据库中。我遇到的问题是,在提交表单后,我无法访问给定的变量和数组(复选框)只有 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>
如果它不为空,我的 php 部分将访问 POST 数据:
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>");
}
}
}
此时我只想在提交后访问变量并回显它们,当这部分工作时,我将执行事务以将数据插入数据库。我将不胜感激任何建议或建议,我被困在这一点上。
(运行第一张图片的结果是数组只有一个元素,而 VAT_doctor、VAT_client 等为空)
解决方案
我通过从动态表单传递帖子变量解决了这个问题。我通过 GET 方法传递的其他变量。我只是设法通过 post 传递了一个变量,我将它作为一个数组传递,其中值作为第一个元素,其余的是空元素。
<form method='post' action='dental_chart.php?VAT_client=$VAT_client_g&VAT_doctor=$VAT_doctor_g&date=$date_g'>
<p>Description:
<input type='text' name='description[]' placeholder='describe procedure'/>
</p>
<hr>
<h4>Insert procedure charting data - gap per tooth:</h4>
<p></p>
<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>
推荐阅读
- angularjs - 如何在 data-ng-options 创建的下拉列表中设置默认选定选项
- java - 在 Java 中处理来自线程的中断
- python - 将具有多个参数的函数的名称从 python 字典调用到另一个函数
- javascript - 执行跳过承诺语句
- vue.js - VueJS - WEBPACK_IMPORTED_MODULE_0___default.a.Textract 不是构造函数
- javascript - JavaScript - 比较字符串并从其中一个字符串中删除第一个字符,直到它们相等
- javascript - 查找不包括少数键的对象的总和
- c# - 如何从 BackgroundJob.Enqueue() 中捕获返回对象?
- plot - 如何将数据缩放添加到 vega 图?
- azure - 无法在 Azure 的“创建事件订阅”中选择资源