我是PHP / jquery的新手
我想问一下如何使用json格式的ajax从表单字段(名称,年龄等)发送json数据.可悲的是,我找不到任何相关的信息,甚至可以动态地做到这一点? Google搜索仅返回手动构建数据等答案.喜欢:名字:X Y,年龄:32,依此类推.@H_404_3@
反正有吗?@H_404_3@
谢谢您的帮助!@H_404_3@
编辑:@H_404_3@
@H_404_3@
<form action="test.PHP" method="post">
Name: <input type="text" name="name"><br>
Age: <input type="text" name="email"><br>
FavColor: <input type="text" name="favc"><br>
<input type="submit">
</form>
@H_404_3@
<?PHP
// this is just a test
//send back to the ajax request the request
echo json_encode($_POST);
这是我的index.html@H_404_3@
@H_404_3@
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form id="form" action="" method="post">
Name: <input type="text" name="name"><br>
Age: <input type="text" name="email"><br>
FavColor: <input type="text" name="favc"><br>
<input id="submit" type="button" name="submit" value="submit">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
// click on button submit
$("#submit").on('click', function(){
// send ajax
$.ajax({
url: 'test.PHP', // url where to submit the request
type : "POST", // type of action POST || GET
dataType : 'json', // data type
data : $("#form").serialize(), // post data || get data
success : function(result) {
// you can see the result from the console
// tab of the developer tools
console.log(result);
},
error: function(xhr, resp, text) {
console.log(xhr, resp, text);
}
})
});
});
</script>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。