How to send an array or JSON through a post on button click

Viewed 31

I want to send an array or better yet a JSON encoded array through a post to my express server and then to download the file into a local txt file on my express server.

<div id="buttons-div">
    <button id="yellow-button" onclick="changeColor('yellow')"></button>
    <button id="red-button" onclick="changeColor('red')"></button>
    <button id="blue-button" onclick="changeColor('blue')"></button>
    <button id="white-button" onclick="changeColor('white')"></button>
    <button id="green-button" onclick="changeColor('green')"></button>
    <form method="POST" action="colors">    
        <input id="submit" href="/colors" type="submit"></input>
    </form>
</div>
function submitColors(e) {
    fs.writeFile("./colors.txt", JSON.encode(e), (err) => {
        if(err){
            console.log(err);
        }
        else{
            console.log("Successfully sent JSON to colors.txt (Array: " + e + " and JSON " + JSON.encode(e));
        }
    });
}
app.get('/colors', function(req, res) {
  submitColors(cArray);
  res.render('index.php');
  res.send('Colors maybe sent successfully?');
});

I'm not sure if there's a better way to do it but this is the only way I can think of it. The issue I am having is that cArray is in another javascript file and I want to use it in my index.js because I need to use fs.writeFile there, unless I'm mistaken, which could be the case since I'm new to this. Any help?

0 Answers
Related