我想做的是:我有一个html表单在页面"add.php"与一些jquery代码发送用户输入到"save.php"使用serialize()函数。现在,在"save.php"文件成功或失败后,我想在调用者页面"add.php"中接收一个数组响应,并使用jquery在页面的某个地方显示它。
到目前为止我所尝试的:我可以将POST数据发送到"save.php",并使用serialize()函数和jquery ajax从它接收单个响应。但我无法接收数组响应从"save.php"使用xml或json。
$(document).ready(function(){
$('.notification').hide();
$('.submit').click(function(){
var check=0;
console.log($('#requestForm').serialize());
if(check == 0){
$.ajax({
url: "save.php",
type:"POST",
dataType:"json",
data: $('#requestForm').serialize(),
statusCode: {404: function() {alert('page not found');}
},
success: function(data){
alert("works"+data);
// alert(data["alert_type"]);
}
});
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form role="form" id="requestForm" method="post" action="save.php">
<input type="text" name="txt_code" id="txt_code" placeholder="Customer Code"/>
<input type="text" name="txt_name" id="txt_name" placeholder="Customer Name"/>
<input type="text" name="txt_searchname" id="txt_searchname" class="form-control" placeholder="Search Name (24)"/>
<button type="button" class="submit">Send Request</button>
</form>
请帮我找到解决这个问题的方法。提前感谢任何建议。
为什么不从php函数返回json ?
http://php.net/manual/fr/function.json-encode.php<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
//The above example will output:
{"a":1,"b":2,"c":3,"d":4,"e":5}