如何将html表单的用户输入保存到单个csv文件中

时间:2017-11-03 14:28:57

标签: html forms csv

我有一个html表单,当用户提交表单时它应该将文件保存为csv和store,当用户再次输入/提交表单时,它应该在同一个csv文件中更新。我使用了下面的代码每次都可以创建一个新的csv文件,任何人都可以告诉我该怎么做。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Writing Data to CSV</title>
    <script type="text/javascript">
        function addToCSVFile() {
            var csvData = new Array();  // To collect the names


            // Collecting the names
            csvData[0] = document.getElementById('firstName').value;
            csvData[1] = document.getElementById('middleName').value;
            csvData[2] = document.getElementById('lastName').value;

            window.open('data:text/csv;charset=utf-8,' + csvData.join(','));

            clearData();
            alert("Data Added Successfully");
        }

        function clearData() {
            document.getElementById('firstName').value = "";
            document.getElementById('middleName').value = "";
            document.getElementById('lastName').value = "";
        }
    </script>
</head>
<body>
    <p>
        First Name: <input type="text" id="firstName" />
        <br />
        Middle Name: <input type="text" id="middleName" />
        <br />
        Last Name: <input type="text" id="lastName" />
        <br />
        <input type="button" id="addButton" value="Add to CSV File" onClick="javascript: addToCSVFile()" />
    </p>
</body>

0 个答案:

没有答案