Php 有没有一种方法可以使用jQuery和AJAX发布表单而不进行任何序列化?
我有一个表单,其中包含动态创建的复选框,用于筛选要显示的数据:Php 有没有一种方法可以使用jQuery和AJAX发布表单而不进行任何序列化?,php,jquery,ajax,Php,Jquery,Ajax,我有一个表单,其中包含动态创建的复选框,用于筛选要显示的数据: $html .= "<form id='optionsForm'action='filter.php' method='post'>"; foreach($selectValues as $key => $value){ $title=new MODEL\String($key); $html.= "<fieldset class='optionsBox'>"; $html.=
$html .= "<form id='optionsForm'action='filter.php' method='post'>";
foreach($selectValues as $key => $value){
$title=new MODEL\String($key);
$html.= "<fieldset class='optionsBox'>";
$html.= "<legend>".$title->friendlify()."</legend><br/>";
foreach($value as $option){
$html .= "<input type='checkbox' name='".$key."[]' value='$option'>".htmlspecialchars($option)."</input>";
}
$html.= "</fieldset>";
}
$html .= "</select><input type='submit' value='submit'></form></div>";
提交后,数组将传递给php函数,该函数将数组分解,然后使用数据运行查询
我的问题是,当我使用jQuery.ajax
函数时,它会序列化数据,而我无法以我想要的方式使用它
使用相同表单操作的最简单方法是什么?我必须处理表单,但结果显示在复选框下方的div
我想更好的问题是当表单操作中使用的filter.php获得信息时,当它来自ajax时,变量名将是什么?
现在我只需要使用_POST变量并将其设置为新数组名
$('#optionsForm').submit(function() {
$.ajax({
url: this.action,
type: "POST"
data: $(this).serialize(),
// ...
// add success/complete/error handlers
});
return false;
});
.serialize()
将格式化数据,就像它是使用提交按钮提交的标准表单一样
单击提交按钮将执行此ajax并阻止默认的表单操作。如果您不喜欢jQuery序列化数据的方式,可以在发布到服务器之前根据表单值准备自己的有效负载。您应该能够按照自己的意愿格式化数据。您只需使用
$.post("test.php?data1=1&data2=2", function(data) {
alert("Data Loaded: " + data);
});
从服务器端,您需要请求每个变量
$.post("test.php?data1=1&data2=2", function(data) {
alert("Data Loaded: " + data);
});