我有一个使用jquery ajax动态生成的表单,并且输入字段名称值是动态生成的,我想用php更新这些字段,我不确定该怎么做。
提交表单时,我不知道输入字段的名称是什么。
<form id="modalform" action="#" method="post">
<input type="text" name="46" class="form-control margin-top20 " value="selcetopt 1" />
<input type="text" name="50" class="form-control margin-top20 " value="selcetopt 2" />
<input type="text" name="56" class="form-control margin-top20 " value="selcetopt 3" />
<input type="text" name="66" class="form-control margin-top20 " value="selcetopt 4" />
<input type="text" name="96" class="form-control margin-top20 " value="selcetopt 5" />
<input type="submit" value="Update" name="submit" />
</form>
答案 0 :(得分:3)
您可以遍历$ _POST以获得所有输入的字段值,根据您的问题,我在下面添加了示例代码
<?php
if(!empty($_POST)){
foreach($_POST as $key => $value){
// Preocess $key which holds name of input field
// You can apply your logic to process value for an input $key here
// From your example it looks like name is a number so special case can check within a condition for $key as number
if(ctype_digit($key)){
// This will only get the value of all dymanic input fields if name is a number
}
}
}
希望对您有帮助
答案 1 :(得分:0)
看看是否有任何帮助,创建这样的表单
<form onsubmit="return submitForm(this);">
<input data-key="field-1" value="" />
<input data-key="field-2" value="" />
<input data-key="field-3" value="" />
...
<button type="submit">Submit form</button>
</form>
jQuery代码:
var form_data = {};
function parseInputs(form) {
$(form).find("input").each(function() {
form_data[$(this).data("key")] = $(this).val();
})
return form_data;
}
function submitForm(form) {
var options = {
type: "POST",
url: "form_handling.php",
data: parseInputs(form),
success: function (response) {alert(response)}
};
$.ajax(options);
return false;
}
form_handling.php
将是:
<?php
foreach($_POST as $key => $value) {
echo $key . " = " . $value;
}
?>