Alejandro Suarez
Alejandro Suarez

Reputation: 168

Get <textarea> data to JSON file on the server?

Hi guys this is more a mixed bag, either question or just point me in the right direction. Im working on a project and i hit a wall know, maybe(most likely) cause of lack of knowledge. So here goes:

I have a JSON file on my web server which data im getting to populate 4 textarea tags with different values on my HTML site...

So the texarea in that site is using the data in the following JSON file which i have on the server and will display on click to the user:

{
"printers" : [ 
            "PRINTER 1",
            "PRINTER 2",
            "PRINTER 3",
            "PRINTER 4",
            "PRINTER 5" 
            ],
"drives" : [
            "DRIVE 1",
            "DRIVE 2",
            "DRIVE 3",
            "DRIVE 4",
            "DRIVE 5"
            ],
"perms" : [
            "PERM 1",
            "PERM 2",
            "PERM 3",
            "PERM 4",
            "PERM 5" 
            ],
"links" : [  
            "LINK 1",
            "LINK 2",
            "LINK 3",
            "LINK 4",
            "LINK 5"
            ]
} 

Uff ok so far im able to do so fine. Now comes the issue...

How can i save changes user make using the 'Save Changes button' done to the data back to that JSON file on the server keeping the same original structure as its relevant to other parts of the site?

How would you approach this situation?

What do i need to study/research? :)

Please if duplicate, pointing in the right direction will be appreciated.

Thanks in advance

PS: Current javascript code can be provided if needed.

Upvotes: 0

Views: 120

Answers (2)

Zack Argyle
Zack Argyle

Reputation: 8407

It is hard without seeing your code, but I would assume you would want to do something like this. This assumes that each text area has an id mapped to your object.

function getResults() {
    var keys = ['printers', 'drives', 'perms', 'links'];
    return keys.reduce(function(result, key) {
        var text = document.getElementById(key).value;
        result[key] = text.split('\n');
        return result;
    }, {})
}

onClick -> getResults() -> Post to server

Upvotes: 1

Keith Rousseau
Keith Rousseau

Reputation: 4475

You'll need to create an api endpoint using your server stack that will accept json in the format that you want. That endpoint can then write to your json file.

From the client side you'll need to get the values from each textarea, split them on newline character into an array and create your JSON structure. You can then make an ajax post to your api endpoint.

Upvotes: 0

Related Questions