Framework7 - Form To JSON



Description

Framework7 allows you to convert all form field values to JSON by using the following method −

  • myApp.formToJSON(form) − This method accepts form as parameter which is a HTMLElement or string of form that must be converted to JSON. This parameter is required and this method returns JSON data.

Each input must have the name attribute, otherwise its value will not appear in JSON. Multiple selects and checkboxes will appear as arrays in JSON.

Example

The following example demonstrates the use of form to JSON in Framework7 −

<!DOCTYPE html>
<html>

   <head>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1, 
         maximum-scale = 1, minimum-scale = 1, user-scalable = no, minimal-ui" />
      <meta name = "apple-mobile-web-app-capable" content = "yes" />
      <meta name = "apple-mobile-web-app-status-bar-style" content = "black" />
      <title>Checkboxes group</title>
      <link rel = "stylesheet" 
         href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.min.css" />
      <link rel = "stylesheet" 
         href = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/css/framework7.ios.colors.min.css" />
   </head>

   <body>
      <div class = "views">
         <div class = "view view-main">
            <div class = "pages">
               <div data-page = "home" class = "page navbar-fixed">
                  
                  <div class = "navbar">
                     <div class = "navbar-inner">
                        <div class = "left"> </div>
                        <div class = "center">Form To JSON</div>
                        <div class = "right"> </div>
                     </div>
                  </div>
                  
                  <div class = "page-content">
                     <form id = "my-form" class = "list-block">
                        <ul>
                           <li>
                              <div class = "item-content">
                                 <div class = "item-inner">
                                    <div class = "item-title label">Name</div>
                                    <div class = "item-input">
                                       <input type = "text" name = "name" placeholder = "Enter your name">
                                    </div>
                                 </div>
                              </div>
                           </li>
                           
                           <li>
                              <div class = "item-content">
                                 <div class = "item-inner">
                                    <div class = "item-title label">E-mail</div>
                                    <div class = "item-input">
                                       <input type = "email" name = "email" placeholder = "Enter your e-mail">
                                    </div>
                                 </div>
                              </div>
                           </li>
                           
                           <li>
                              <div class = "item-content">
                                 <div class = "item-inner">
                                    <div class = "item-title label">Gender</div>
                                    <div class = "item-input">
                                       <select name = "gender">
                                          <option value = "male" selected>Male</option>
                                          <option value = "female">Female</option>
                                       </select>
                                    </div>
                                 </div>
                              </div>
                           </li>
                           
                           <li>
                              <div class = "item-content">
                                 <div class = "item-inner">
                                    <div class = "item-title label">Switch</div>
                                    <div class = "item-input">
                                       <label class = "label-switch">
                                          <input type = "checkbox" name = "switch" value = "yes" />
                                          <div class = "checkbox"></div>
                                       </label>
                                    </div>
                                 </div>
                              </div>
                           </li>
                        </ul>
                     </form>
                     
                     <div class = "content-block"><a href = "#" class = "button button-fill button-round color-red form-to-json">Get Form Data</a></div>
                  </div>
                  
               </div>
            </div>
         </div>
      </div>
      
      <script type = "text/javascript" 
         src = "https://cdnjs.cloudflare.com/ajax/libs/framework7/1.4.2/js/framework7.min.js"></script>
         
      <script>
         var myApp = new Framework7();

         var $$ = Dom7;

         $$('.form-to-json').on('click', function() {
            var formData = myApp.formToJSON('#my-form');
            alert(JSON.stringify(formData));
         });
      </script>
   </body>

</html>

Output

Let us carry out the following steps to see how the above given code works −

  • Save the above given HTML code as forms_to_json.html file in your server root folder.

  • Open this HTML file as http://localhost/forms_to_json.html and the output is displayed as shown below.

  • When you enter your details in the form and click on ‘Get form data’ button, all your field values will be converted to JSON format and is displayed to you.

framework7_forms.htm
Advertisements