1

So i have a HTML page that asks the user the name, the description and the image of an activity (like a formular). I want to make a function that saves all the user input into the data.json file when the user clicks on the button but i don't know how to do it. Here the function about the button in js :

function send() {
var nomActi = document.getElementById("nomActi").value;
var descriActi = document.getElementById("descriActi").value;
var imageActi = document.getElementById("imageActi").value;
}

1 Answer 1

1

Heres how you can write to a json file.

fs - module to write it to a file

const fs = require('fs');

// create a JSON object
const user = {
    "id": 1,
    "name": "John Doe",
    "age": 22
};

// convert JSON object to string
const data = JSON.stringify(user);

// write JSON string to a file
fs.writeFile('user.json', data, (err) => {
    if (err) {
        throw err;
    }
    console.log("JSON data is saved.");
});

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

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.