简体   繁体   English

如何将html表单导出到csv文件

[英]How to export html form to csv file

I am trying to create a form that a user can enter data into and then when they click submit, that data will be added as a new row to a csv file stored on the server which I can then download. 我正在尝试创建一个用户可以输入数据的表单,然后当他们单击提交时,该数据将作为新行添加到存储在服务器上的csv文件,然后我可以下载。 I have tried both some php stuff and javascript, both of which seem to be good, but I am not very experienced in either. 我已经尝试了一些php的东西和javascript,两者似乎都很好,但我也不是很有经验。

Here is basicaly the html form: 这是html表单的基本内容:

<form name="xyz_form">
    <section class="border-bottom">
<div class="content">
    <h3>ID Number</h3>
    <div class="form-control-group">
        <div class="form-control form-control-number">
            <input type="number" id="id_number">
        </div>
    </div>
            <h3>First Name</h3>
    <div class="form-control-group">
        <div class="form-control form-control-text">
            <input type="text" id="first_name">
        </div>
    </div>
</div><!--.content-->
    <section class="data-capture-buttons one-buttons">
       <div class="content">
          <input type="submit" value="Submit" onClick="javascript:addToCSVFile()">
       </div>
    </section><!--.data-capture-buttons-->

The Javascript is in the header of the html file and it is: Javascript位于html文件的标题中,它是:

<script type="text/javascript">
        function addToCSVFile() {
            var csvData = new Array();  // To collect the names
            var csvFilePath = "Data.csv"; // File name

            // Collect General Information
            csvData[0] = document.getElementById('id_number').value;
            csvData[1] = document.getElementById('first_name').value;

              var fso = new ActiveXObject('Scripting.FileSystemObject');
            var oStream = fso.OpenTextFile(csvFilePath, 8, true, 0);
            oStream.WriteLine(csvData.join(','));
            oStream.Close();
            clearData();
            alert("Data Added Successfully");
     }

     function clearData() {
            document.getElementById('id_number').value = "";
            document.getElementById('first_name').value = "";
                     }
    </script>

And right now I have the onClick="javascript:addToCSVFile()"> but it also doesn't work when I set the form action to the handler.php file and the mothod to post and remove the onlick. 现在我有onClick="javascript:addToCSVFile()">但是当我将表单操作设置为handler.php文件并发布并删除onlick时,它也不起作用。 This is the php file. 这是php文件。

   <?
    if(isset($_POST['match_scouting'])){
       $del = "\t";
     //Collect Info
     $data[1] = $_POST['id_number'];
     $data[2] = $_POST['first_name'];
    $file = fopen("Data.csv", "a");
    $data = "\r\n".implode($del, $data);
    fwrite($file, $data);
    fclose($file);
    echo "The data has been added successfully";
 }else{
   header("location: form.html");
 }
 ?> 

If I am doing this wrong, could you point me in a different direction? 如果我做错了,你能指出我的方向吗? What I am ultimately trying to do however is save the form data somehow and I thought this would be easier then setting up an SQL Database since I have never done that before. 然而,我最终要做的是以某种方式保存表单数据,我认为这比设置SQL数据库更容易,因为我以前从未这样做过。

For this you do not need to apply any javascript code. 为此,您无需应用任何JavaScript代码。 Just add an action to your form 只需在表单中添加操作即可

<form name="xyz_form" action="proces.php" method="get">

submit will now redirect you to that php file. 提交现在会将您重定向到该php文件。 that php file should contain this: 那个php文件应该包含这个:

<?php
    $keys = array('id_number','first_name');
    $csv_line = array();
    foreach($keys as $key){
        array_push($csv_line,'' . $_GET[$key]);
    }
    $fname = 'file_to_write_to.csv';
    $csv_line = implode(',',$csv_line);
    if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;}
    $fcon = fopen($fname,'a');
    $fcontent = $csv_line;
    fwrite($fcon,$csv_line);
    fclose($fcon);
?>

$Keys is all the names of the input fields you have, and you can add as many as you'd like. $Keys是您拥有的输入字段的所有名称,您可以根据需要添加任意数量。 $fname is the name of the file you want to write to. $fname是您要写入的文件的名称。 The csv file gets added new lines when the form is submitted. 提交表单时,csv文件会添加新行。

You could look after this: Write a text file and force to download with php . 你可以照顾这个: 写一个文本文件,强制下载PHP If you want to force download the file. 如果要强制下载该文件。 You can also look into this for a redirect instead of download: How to make a redirect in PHP? 您还可以查看此重定向而不是下载: 如何在PHP中进行重定向? . You could also add: 您还可以添加:

echo file_get_contents($fname);

to your PHP 到您的PHP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM