使用jquery将表单数据保存到本地json文件中

时间:2022-10-22 23:26:02

I have a basic form with some input fields. I want to save the form data into a json file on submitting the form. The format of the saved data in the json file should be like this.

我有一个带有一些输入字段的基本表单。我想在提交表单时将表单数据保存到json文件中。 json文件中保存数据的格式应如下所示。

[
    {"title":"some text","description":"some text","info":"some text","username":"some name"},
    {"title":"some text","description":"some text","info":"some text","username":"some name"},
    {"title":"some text","description":"some text","info":"some text","username":"some name"}
]

I tried doing this by using this code, but no data is saved in my 'story.json file'

我尝试使用此代码执行此操作,但没有数据保存在我的'story.json文件'中

$('form').submit(function() {
    $.ajax({
       type: 'POST', 
       dataType: 'json', 
       url: 'story.json', 
       data: $(this).serialize(), 
       success: function(data) { 
           alert(data.message); 
       },
       failure: function (data) {
           alert('Please try again');
       }
    });
 });

I want to save this form data on my local file. Please help me to find the correct way of doing this. Thanks

我想将此表单数据保存在我的本地文件中。请帮我找到正确的方法。谢谢

3 个解决方案

#1


3  

You need to post data to a simple php file...

你需要将数据发布到一个简单的php文件中......

like url: 'story.php' In that php file create a 'story.json' using fopen and store that json

像url:'story.php'在那个php文件中使用fopen创建一个'story.json'并存储json

EDIT: if you want to use serialize() than use someting like this

编辑:如果你想使用serialize()而不是使用像这样的东西

data:{'mydata':$(this).serialize()}

and in php file

并在PHP文件中

parse_str($_POST['mydata'],$newarray) ;

echo json_encode($newarray);

#2


2  

JavaScript cannot save to a file unless it's a FireFox plugin.

JavaScript无法保存到文件,除非它是FireFox插件。

What you do is post a form (sent it to the webserver) then let server side script handle it.

你做的是发布一个表单(发送到网络服务器),然后让服务器端脚本处理它。

Serialize does not turn the form values into a JSON string:

Serialize不会将表单值转换为JSON字符串:

http://api.jquery.com/serialize/

And when you use $.post then you have to return false in the $('form').submit(function() or the browser will submit the form for you.

当你使用$ .post然后你必须在$('form')中返回false。submit(function()或浏览器将为你提交表单。

Submitting a form is when you click a button and the whole page goes white for a moment and then you get a new page.

提交表单是指当您单击按钮并且整个页面变白并且然后您获得新页面时。

Ajax ($.post, $.get, $.getJson ...) is when you send information to the server without refreshing the page. Google maps is an excellent example.

Ajax($ .post,$ .get,$ .getJson ...)是指您在不刷新页面的情况下向服务器发送信息的时间。 Google地图就是一个很好的例子。

#3


0  

Calling the serialize method on the form jQuery object does not return a JSON representation of its data. It returns a querystring representation of its data. As has been mentioned above, you will need to use a server side script to interpret (and manipulate) sent data and store it in a file as JSON.

在窗体jQuery对象上调用serialize方法不会返回其数据的JSON表示。它返回其数据的查询字符串表示。如上所述,您需要使用服务器端脚本来解释(和操作)发送的数据,并将其作为JSON存储在文件中。

#1


3  

You need to post data to a simple php file...

你需要将数据发布到一个简单的php文件中......

like url: 'story.php' In that php file create a 'story.json' using fopen and store that json

像url:'story.php'在那个php文件中使用fopen创建一个'story.json'并存储json

EDIT: if you want to use serialize() than use someting like this

编辑:如果你想使用serialize()而不是使用像这样的东西

data:{'mydata':$(this).serialize()}

and in php file

并在PHP文件中

parse_str($_POST['mydata'],$newarray) ;

echo json_encode($newarray);

#2


2  

JavaScript cannot save to a file unless it's a FireFox plugin.

JavaScript无法保存到文件,除非它是FireFox插件。

What you do is post a form (sent it to the webserver) then let server side script handle it.

你做的是发布一个表单(发送到网络服务器),然后让服务器端脚本处理它。

Serialize does not turn the form values into a JSON string:

Serialize不会将表单值转换为JSON字符串:

http://api.jquery.com/serialize/

And when you use $.post then you have to return false in the $('form').submit(function() or the browser will submit the form for you.

当你使用$ .post然后你必须在$('form')中返回false。submit(function()或浏览器将为你提交表单。

Submitting a form is when you click a button and the whole page goes white for a moment and then you get a new page.

提交表单是指当您单击按钮并且整个页面变白并且然后您获得新页面时。

Ajax ($.post, $.get, $.getJson ...) is when you send information to the server without refreshing the page. Google maps is an excellent example.

Ajax($ .post,$ .get,$ .getJson ...)是指您在不刷新页面的情况下向服务器发送信息的时间。 Google地图就是一个很好的例子。

#3


0  

Calling the serialize method on the form jQuery object does not return a JSON representation of its data. It returns a querystring representation of its data. As has been mentioned above, you will need to use a server side script to interpret (and manipulate) sent data and store it in a file as JSON.

在窗体jQuery对象上调用serialize方法不会返回其数据的JSON表示。它返回其数据的查询字符串表示。如上所述,您需要使用服务器端脚本来解释(和操作)发送的数据,并将其作为JSON存储在文件中。