[英]POST array with jQuery and Ajax
I have a form with multiple fields, whereby fields can be added. 我有一个包含多个字段的表单,可以在其中添加字段。
The FORM is as follows: 表格如下:
<form id="myForm" action="myfile.php" >
<input . . >
<select>. . . </select>
</form>
myfile.php has the following code: myfile.php具有以下代码:
foreach ($_POST as $key => $value) {
echo $key." takes the <b>".$value."</b> Value";
}
This simple code processes all the entries of the form regardless how many. 这个简单的代码处理该表单的所有条目,无论有多少。
Now, what I want is: 现在,我想要的是:
When I click on the submit button, instead of sending the form's content to a script, to actually get that array and pass it AJAX without having to write every single key and its value manually. 当我单击提交按钮时,而不是将表单的内容发送到脚本,而是实际获取该数组并将其传递给AJAX,而不必手动编写每个键及其值。
I hope it makes sense 我希望这是有道理的
I think what you are looking for is serialize() 我认为您正在寻找的是serialize()
jQuery(function ($) {
//submit handler
$('#myForm').submit(function (e) {
//prevent default submit
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: $(this).serialize(),
....
})
});
})
You can use jQuery serialize()
method 您可以使用jQuery
serialize()
方法
$("#myForm").submit(function(e){ //To listen submit event
e.preventDefault(); //To prevent default browser action
var data=$(this).serialize(); // To get form data
$.post($(this).attr('action'),data,function(data){ //To perform post
$('#result').html(data); //Result data
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.