0

Hey guys I have a form where the output is saved in database. What I want is to save it in a json string and show the string.

<form>
  First name:<br>
  <input type="text" name="firstname">
  <br>
  Last name:<br>
  <input type="text" name="lastname">
    <select name="gender" >
        <option value=""></option>
        <option value="Man">Man</option>
        <option value="Vrouw">Vrouw</option>
    </select>
    adress:<br>
  <input type="text" name="adress">
  <input type="submit" class="submit" value="Submit!" />
</form>   


  {
    "PersonalData" : {
        "Gender" : [value of gender]
        "FirstName" : "[valeu of firstname]",
        "adress" : "[valeu of adress]",
}
curo
  • 1
  • 4

2 Answers2

0

Create a JSONObject and add all the values you want into it with the put() method. Then you can use the .toString() function to turn it into a string.

0

$(document).ready(function(){
  var result = {};
  var PersonalData = {};
  
  $("#submitt").click(function(){
    PersonalData.FirstName = $('#first').val();
    PersonalData.Address = $('#address').val();
    PersonalData.Gender = $("#gender option:selected").text();
    
    result.PersonalData = PersonalData;
    console.log(result);
    var resultString = result.toString();
    console.log(resultString);
  });
  

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  First name:<br>
  <input type="text" name="firstname" id="first">
  <br>
  Last name:<br>
  <input type="text" name="lastname">
    <select name="gender" id="gender" >
        <option value=""></option>
        <option value="Man">Man</option>
        <option value="Vrouw">Vrouw</option>
    </select>
    <br>
    Address:<br>
  <input type="text" name="adress" id="address">
  <input type="submit" class="submit" value="Submit!" id="submitt" />
</div>
Prabhakaran
  • 1,524
  • 2
  • 13
  • 21