如何将表单作为JSON对象提交 [英] how to submit form as JSON object
问题描述
我正在做的是使用JSON创建表单然后可以编辑该表单并生成新的JSON对象。我遇到的问题似乎是获取表单ID。
我用来返回JSON对象的代码是:
what I am doing is creating a form using JSON this form can then be edited a and produce new JSON object. The problem I am having seems to be with getting the form id. The code I am using to return a JSON object is:
form = document.forms[0];
$.fn.serializeObject = function()
{
alert("start serializeObject");
var o = {};
var a = this.seralizeArray();
$.each(a, function(){
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
alert(o);
};
$(function() {
alert("here");
form.submit(function(){
result.append(JSON.stringify(form.serializeObject()));
return false;
});
});
这只是刷新页面我不知道为什么。
此程序不在服务器上,不能在服务器上使用。我的意思是
这意味着它只会在本地机器上本地运行,没有apache2设置。
This just refresh's the pageI am not sure why. This program is not on a server and not be used on a server. by this I mean It is only every going to be run locally on a local machine, with no apache2 setup.
谢谢。
推荐答案
你可以很容易地编写代码。我就是这样做的:
You code can be written pretty easy. This is how I do it:
Ajax:
$('#formID').on('submit',function () {
$.ajax({
url: 'submit.php',
cache: false,
type: 'POST',
data : $('#formID').serialize(),
success: function(json) {
alert('all done');
}
});
});
如果你不是用Ajax发送它,你为什么要这样做?如果您只是提交表单,可以使用PHP这样做:
If you are not sending it with Ajax, why would you do this? If you are simply submitting the form, you can do it using PHP like this:
<?php
$json_object = json_decode($_POST);
?>
这篇关于如何将表单作为JSON对象提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!