我使用jQuery在表中生成字段,无法弄清楚如何在(post)提交中检索这些值以插入到数据库中。
这是一个jsFiddle来演示我如何生成字段:
https://jsfiddle.net/hnj6ed1y/26/
我是PHP的初学者,所以除了这个之外我没有太多代码可以使用。我尝试使用DOM getElementById,但由于我使用的是PHP文件,因此无法加载HTML。
if(isset($_POST['back']) || isset($_POST['next'])){
if(isset($_POST['next'])){
$url = "$nextpage";
$dom = new DOMDocument();
$dom->validate();
$divs = $dom->getElementsById('report_cause1');
foreach ($divs as $div) {
foreach ($div->attributes as $attr) {
$name = $attr->nodeName;
$value = $attr->nodeValue;
echo "Attribute '$name' :: '$value'<br />";
}
}
}
else {
$url = "$previouspage";
}
//header("location:$url");
//exit();
}
这是jQuery / JavaScript:
var count_2 = 5;
var table_count2 = 1;
$(document).ready(function () {
$('#btnAdd').click(function () {
count_2 = count_2 + 2;
table_count2 = table_count2 + 1;
$('#Table tr:last').after('<tr><td>' + table_count2 + '</td>' +
'<td>' +
'<select name=\"report_cause' + count_2 + '\" id=\"report_cause' + count_2 + '\" class=\"report_cause\">' +
'<option value=\'default\'>-</option>' +
'<option value=\'temp_not_rec\'>Temperatures not recorded</option>' +
'<option value=\'other\'>Other, Not Listed</option>' +
'</select>' +
'<p class=\"cause_details\" style=\"display:none\">' +
'<input type=\"text\" name=\"cause_detail_info' + count_2 + '\" id=\"cause_detail_info' + count_2 + '\" placeholder=\"Type cause here\" size=\"48\" maxlength=\"50\" class=\"textbox required\" value=\"\" />' +
'</p>' +
'</td>' +
'<td>' +
'<p class=\"null_temp\">' +
'<input type=\"text\" name=\"temp' + count_2 + '\" id=\"temp' + count_2 + '\" placeholder=\"Temp\" size=\"5\" maxlength=\"5\" value=\"\" />' +
'</td></tr>');
});
});
HTML:
<table style="width: 70%" id="Table" name="Table">
<tr>
<th>#</th>
<th>Cause</th>
<th>Temp</th>
</tr>
<tr>
<td>1</td>
<td>
<select name='report_cause2' id='report_cause2' class="report_cause">
<option value='default'>-</option>
<option value='temp_not_rec'>Temperatures not recorded</option>
<option value='other'>Other, Not Listed</option>
</select>
</td>
<td>
<p class="null_temp">
<input type="text" name="temp1" id="temp1" placeholder="Temp" size="5" maxlength="5" value="" />
</p>
</td>
</tr>
</table>
<input type="button" id="btnAdd" name="btnAdd" value="Add More" class="button" />
<input type="button" name="back" value="Back" class="button" onclick="goBack()" />
<input type="submit" name="next" value="Next" class="button" />
CSS:
#Table {
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse:collapse;
}
#Table td, #Table th {
font-size:1em;
border:1px solid #98bf21;
padding:3px 7px 2px 7px;
}
#Table th {
font-size:1.4em;
text-align:left;
padding-top:5px;
padding-bottom:4px;
background-color:#A7C942;
color:#fff;
}
#Table tr.alt td {
color:#000;
background-color:#EAF2D3;
}
提前谢谢大家!
答案 0 :(得分:2)
理论上,您的表单元素应该保存在$ _POST数组中,只要您的字段包含在$_POST['report_cause2']
元素中,就可以使用命令$_POST['cause_detail_info2']
和<form>
进行访问action="<your php script>"
和method="POST"
。
答案 1 :(得分:1)
包装你的表&amp;输入
在form
标记中查看
更多:http://www.w3schools.com/tags/tag_form.asp
使用全局变量$ _POST来获取字段的值,请参阅:http://php.net/manual/ro/reserved.variables.post.php