My form contains hidden input looping. in my case, i declare the hidden input in ajax data manually without looping. so how to loop them in ajax data?
我的表单包含隐藏的输入循环。在我的情况下,我手动声明ajax数据中的隐藏输入而不循环。那么如何在ajax数据中循环它们呢?
here's my form script
这是我的表单脚本
<form method="POST" name="myform">
<?php for($i=1;$i<=5;$i++) { ?>
<input type="hidden" name="data<?php echo $i; ?>" value="data<?php echo $i; ?>">
<?php } ?>
<input type='button' name='submitData' value='Submit' onClick='submitData();'>
</form>
here's my Ajax script
这是我的Ajax脚本
function submitData() {
var form = document.myform;
$.ajax({
url: 'process.php',
type: 'post',
data: {
data1 : form["data1"].value,
data2 : form["data2"].value,
data3 : form["data3"].value,
data4 : form["data4"].value,
data5 : form["data5"].value
},
success: function (result) {
console.log(result);
},
error: function () {
console.log("error");
}
});
}
3 个解决方案
#1
3
Your hidden inputs have name and values,
隐藏的输入有名称和值,
use .serialize()
Encode a set of form elements as a string for submission
将一组表单元素编码为字符串以进行提交
data : $('form[name=myform]').serialize()
This will return name=value
pairs.
这将返回name = value对。
If you need {name:value}
, use .each()
如果您需要{name:value},请使用.each()
var formData = {}
$('form :input:hidden[name^="data"]').each(function(){
formData[this.name] = this.value;
});
And in ajax,
在ajax中,
data : formData ,
#2
2
If you're posting the whole form, you can use jQuery's .serialize() directly in your request:
如果您要发布整个表单,可以直接在请求中使用jQuery的.serialize():
$.ajax({
url: 'process.php',
type: 'post',
data: $('#myform').serialize(),
...
#3
1
Also, a good way to do it is to convert the string to JSON object, and in PHP convert the string in an array:
此外,一个好方法是将字符串转换为JSON对象,并在PHP中转换数组中的字符串:
var dataJSON = JSON.stringify({..Your form obj...});
$.ajax({
url: 'process.php',
type: 'post',
data: dataJSON,
success: function (result) {
console.log(result);
},
error: function () {
console.log("error");
}
});
process.php
$data = json_decode($_POST["data"]);
print_r($data);
#1
3
Your hidden inputs have name and values,
隐藏的输入有名称和值,
use .serialize()
Encode a set of form elements as a string for submission
将一组表单元素编码为字符串以进行提交
data : $('form[name=myform]').serialize()
This will return name=value
pairs.
这将返回name = value对。
If you need {name:value}
, use .each()
如果您需要{name:value},请使用.each()
var formData = {}
$('form :input:hidden[name^="data"]').each(function(){
formData[this.name] = this.value;
});
And in ajax,
在ajax中,
data : formData ,
#2
2
If you're posting the whole form, you can use jQuery's .serialize() directly in your request:
如果您要发布整个表单,可以直接在请求中使用jQuery的.serialize():
$.ajax({
url: 'process.php',
type: 'post',
data: $('#myform').serialize(),
...
#3
1
Also, a good way to do it is to convert the string to JSON object, and in PHP convert the string in an array:
此外,一个好方法是将字符串转换为JSON对象,并在PHP中转换数组中的字符串:
var dataJSON = JSON.stringify({..Your form obj...});
$.ajax({
url: 'process.php',
type: 'post',
data: dataJSON,
success: function (result) {
console.log(result);
},
error: function () {
console.log("error");
}
});
process.php
$data = json_decode($_POST["data"]);
print_r($data);