0

I want to save ( store ) some data from HTML input forms to DB.Json file when i click Insert button from html input form !

This is what i have HTML :

<table id="datatable" class="display" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Age</th>  
<th>Birthday</th>
<th>Action</th>
</tr>
</thead>
<tr>
<td><input type="text" name="first_name" id="first_name"></td>
<td><input type="text" name="last_name" id="last_name"></td>
<td><input type="text" name="age" id="age"></td>
<td><input type="text" name="birthday" id="birthday"></td>
<td><button type="button" onclick="insert1()">Insert</button></td>
</tr>

now when i fill all fields on html index and click the insert button need to save these data's on json file

1
  • Missing info here. Do you want to submit to a backend and save it there to file or to a DB or do you simply want to collect the info on the client as JSON? If you are looking at sending it to a backend, what backend? Commented Apr 27, 2019 at 21:05

2 Answers 2

1

First of all, place your <tr> inside <tbody> (it's always a good practice).

Now about your issue. You can't create a separate DB.json file with javascript, but you can create a JSON object and store values in that.

Then just build your Json Objectby getting balues by every input id

function insert1() {
  myJson = {
    first_name: $("#datatable").find("#first_name").val(),
    last_name: $("#datatable").find("#last_name").val(),
    age: $("#datatable").find("#age").val(),
    birthday: $("#datatable").find("#birthday").val(),
  };
  
  console.log("myJson", myJson);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table id="datatable">
  <thead>
    <tr>
      <th>Name</th>
      <th>Surname</th>
      <th>Age</th>  
      <th>Birthday</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><input type="text" name="first_name" id="first_name"></td>
      <td><input type="text" name="last_name" id="last_name"></td>
      <td><input type="text" name="age" id="age"></td>
      <td><input type="text" name="birthday" id="birthday"></td>
      <td><button type="button" onclick="insert1()">Insert</button></td>
    </tr>
  </tbody>
</table>

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your time to help me, but i want the input array to save on the DB.json File, i fill all the input forms and click the insert when i check the DB.json file the inputs didnt save ! can you help me on this
0

you can use axios to write in json file

simply it's an ajax wrapper

axios.post("json file path", jsonObject).then(
  res => console.log(res); /// successfully writing in file
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.