0

Trying to submit form data (name and lastname) to a csv file which is stored in the same directory as the html file. Has to be either JavaScript or JQuery because im injecting this into a SquareSpace website which only supports JS.

So far I have been able to serialize the form data into an array which returns

0: {name: "FirstName", value: "John"}
1: {name: "LastName", value: "Doe"}

Spent a couple of hours trying to save this array information into my comments.csv file, to no avail.

Here's the code:

<form id="form">
        Type your first name: <input type="text" name="FirstName" size="20"><br>
        Type your last name: <input type="text" name="LastName" size="20"><br>
        <input type="button" value="submit" id="dl" onclick="clearForm();">
      </form>

    <script>
//Clear form once submitted
function clearForm(){
    document.getElementById("form").reset();
}

$(document).ready(function(){  
    $("button").click(function(){  
        var x = $("form").serializeArray();  
    });                                     
});  

//Submit x array into ./comments.csv file


    </script>

I want to get the firstname and lastname underneath appropriate columns in the csv file. Firstname being 1st column and lastname being 2nd column, in a way that I will be able to display this information in an html table later on.

1 Answer 1

3

You have to parse your form data into CSV format and trigger a link to download. See implementation below

       <form id="form">
          Type your first name: <input type="text" name="FirstName" size="20"><br>
          Type your last name: <input type="text" name="LastName" size="20"><br>
          <input type="button" value="submit" id="dl" onclick="submitForm()">
        </form>

        <script>
          //Clear form once submitted
          function clearForm(){
              document.getElementById("form").reset();
          }

          function submitForm() {
            var formData = $("form").serializeArray();
            let csv = "data:text/csv;charset=utf-8,"; // accept data as CSV

            formData.forEach(function(item) {
              csv += item.value + ";"; // concat form value on csv var and add ; to create columns (you can change to , if want)
            });

            var encodedUri = encodeURI(csv);

            // if you want to download
            var downloadLink = document.createElement("a");
            downloadLink.setAttribute("download", "FILENAME.csv");
            downloadLink.setAttribute("href", encodedUri);
            document.body.appendChild(downloadLink); // Required for FF
            downloadLink.click();
            downloadLink.remove();

            clearForm();
          }
        </script>
Sign up to request clarification or add additional context in comments.

5 Comments

event is not a global object in all browsers. Will throw error in many of them
ok I removed the line because it was useless, because the form uses type button
I don't want it to download the csv file though. I already have the csv file created and just want the form content to be inserted into the csv file if that's possible.
If you already have a file is impossible edit him with javascript. You need a implementation with server side language.
"csv += item.value + ";"; // concat form value on csv var and add ; to create columns (you can change to , if want)" — This is a really unreliable way of encoding data to CSV that fails to account for any special characters. Use a robust library like Papa Parse instead.

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.