2015-12-19 42 views
0

我已經在html中創建了一個表單,並且我想將其用戶插入的數據以JSON對象的形式存儲在JSON文件中&數組,我與json &一起使用JavaScript so friendly告訴我該怎麼做?將表單數據存儲到json文件中

這是我的HTML表單:

<!DOCTYPE html> 
<html> 
<body> 

<form> 
    First name:<input type="text" name="firstname">Last name:<input type="text" name="lastname"><br> 
    Gender: <input type="radio" name="gender">male<input type="radio" name="gender">female<br> 
    Married: <input type="checkbox" name="married">yes<input type="checkbox" name="merried">no<br> 
    Degree: 
    <select> 
     <option>BS</option> 
     <option>MS</option> 
     <option>PhD</option> 
    </select> 
    Description: <textarea style="width:200px"></textarea> 
</form> 

</body> 
</html> 

,我想輸出以.json文件中像這樣:

[ 
    { 
     "first name": "adam", 
     "last name": "smith" 
     "gender": "male", 
     "merried": "yes" 
     "degree": "MS", 
     "description": "any discription" 

    }, 
    { 
     "first name": "anglina", 
     "last name": "jouley" 
     "gender": "female", 
     "merried": "no" 
     "degree": "PhD", 
     "description": "any discription" 
    }, 
] 

回答

2

你不能只是將它保存到文件系統在前端使用javascript和html。爲此你需要一個後端。

步驟應該是:

  1. 序列化形式的數據使用jQuery或純JavaScript來JSON。
  2. 通過ajax/xmlhttprequest將json數據發送到後端服務器。
  3. 在服務器端接收JSON數據並保存到一個文件以.json
相關問題