将html表单输入保存到json文件

时间:2016-08-15 21:08:09

标签: javascript jquery html css json

<div class="email">
<section class="subscribe">
<div class="subscribe-pitch">
</div>
<form action="#" method="post" class="subscribe-form" id="emails_form">
<input type="email" class="subscribe-input" placeholder="Enter email for newsletter" >
<button id="email_submit" type="submit" value="send" class="subscribe-submit"><i class="fa fa-chevron-right"></i></button>
</form>

我需要将输入数据从简单的电子邮件表单保存到json文件。我想我是用javascript做的。有人可以一步一步帮忙吗?我是新手

3 个答案:

答案 0 :(得分:4)

DEMO

使用序列化我们可以将输入html表单保存到JSON输出

<script type="text/javascript">
  $(document).ready(function() {
  $("#btn").click(function(e){
     var jsonData = {};

   var formData = $("#myform").serializeArray();
  // console.log(formData);

   $.each(formData, function() {
        if (jsonData[this.name]) {
           if (!jsonData[this.name].push) {
               jsonData[this.name] = [jsonData[this.name]];
           }
           jsonData[this.name].push(this.value || '');
       } else {
           jsonData[this.name] = this.value || '';
       }


   });
   console.log(jsonData);
    $.ajax(
    {
        url : "action.php",
        type: "POST",
        data : jsonData,

    });
    e.preventDefault(); 
});
});
</script>

<强> HTML

<div id="header">
 Send Form's data as JSON
</div>

<form id="myform" type="post">
  <fieldset>
    <legend>Ajax Form  </legend>
    <p>We would love to hear from you. Please fill out this form</p>
    <div class="elements">
      <label for="name">Name :</label>
      <input  required="required" type="text"  value="Girish Kumar Santhu" name="fname"  size="20"  />
    </div>
     <div class="elements">
      <label for="Age">Age :</label>
      <input required="required" type="number" value="32" id="age" name="age" size="10" />
    </div>  
    <div class="elements">
      <label for="pro"> Profession :</label>
      <select name="pro">
   <option value="Student">Student</option>
   <option value="Engineer" selected="selected">Engineer</option>
   </select>
    </div>      
    <div class="elements">
    <label for="Gender">Gender: </label>
      <input type="radio" name="gender" value="Male" checked="checked" id="r1"> Male 
  <input type="radio" name="gender" value="Female" id="r2"> Female 
</div>  
    <div class="elements">
      <label for="hobby">Hobby :</label>
      <input type="checkbox" name="hobby[]" value="Sports" id="ch1" checked="checked"> Sports 
  <input type="checkbox" name="hobby[]" value="Coding"  id="ch2"> Coding 
   </div>

    <div class="submit">
       <input type="submit" id="btn" name="btn" class="btn" value="Submit" />
    </div>
    <span class="elements">Press "Ctrl + Shift + J" to see sent JSON in console: <span>
  </fieldset>
</form>

答案 1 :(得分:2)

你需要使用

'use strict';

 const fs = require('fs');

let student = {  
                    name: 'Mike',
                    age: 25, 
                    gender: 'Male',
                    department: 'English',
                    car: 'Honda' 
                };

                let data = JSON.stringify(student);  

fs.writeFileSync('file.json', data, finished);

                function finished(err)
                {
                    console.log('success');
                }

答案 2 :(得分:0)

使用jQuery,它非常简单:

O(log(N))

如果要将var formData = JSON.stringify($("#emails_form").serializeArray()); 存储在JSON文件中,则需要将其发布到服务器(例如,按照AJAX)并保存。但在这种情况下,您只需发布表单并将其转换为服务器本身的JSON即可。

请参阅this answer