如何将html格式的数据输入导出到.csv文件?

时间:2018-01-15 08:24:58

标签: php html export-to-csv

我是新手,初学者,根据我的需要通过搜索和编辑从互联网获取一些代码,但现在我想将html格式的数据条目保存到data.csv文件,当我点击提交按钮时,它应该保存每个点击提交按钮的新行中的data.csv内容。这个HTML代码我只运行本地计算机。我不知道我是怎么做MWE的。提前感谢您的任何帮助

 <html>

   <head>

   <script type="text/javascript" src="validate.js"></script>

   </head>

   <body>

   <form action="#" name="StudentRegistration" onSubmit="return(validate());">


 <table cellpadding="2" width="20%" bgcolor="99FFFF" align="center"

   cellspacing="2">

 <tr>

   <td colspan=2>

   <center><font size=4><b>Student Registration Form</b></font></center>

   </td>

   </tr>

 <tr>

   <td>Name</td>

   <td><input type=text name=textnames id="textname" size="30"></td>

   </tr>

 <tr>

   <td>Father Name</td>

   <td><input type="text" name="fathername" id="fathername"

   size="30"></td>

   </tr>

   <tr>

   <td>Postal Address</td>

   <td><input type="text" name="paddress" id="paddress" size="30"></td>

   </tr>

 <tr>

   <td>Personal Address</td>

   <td><input type="text" name="personaladdress"

   id="personaladdress" size="30"></td>

   </tr>

 <tr>

   <td>Sex</td>

   <td><input type="radio" name="sex" value="male" size="10">Male

   <input type="radio" name="sex" value="Female" size="10">Female</td>

   </tr>

 <tr>

   <td>Stream</td>

   <td><select name="Stream">

   <option value="-1" selected>Select..</option>

   <option value="ARTS">ARTS</option>

   <option value="COMMERCE">COMMERCE</option>

   <option value="SCIENCE">SCIENCE</option>

   </select></td>

   </tr>

 <tr>

   <td>Core-1</td>

   <td><select name="Core-1">

   <option value="-1" selected>Select..</option>

   <option value="English">English</option>

   </select></td>

   </tr>
 <tr>

   <td>Core-2</td>

   <td><select name="Core-2">

   <option value="-1" selected>Select..</option>

   <option value="Alternative English">Alternative English</option>

   </select></td>

   </tr>

 <tr>

 <td>Elective-1</td>

   <td><select name="Elective-1">

   <option value="-1" selected>Select..</option>

   <option value="MIL (Hindi)">MIL (Hindi)</option>
   <option value="MIL (Bengali)">MIL (Bengali)</option>
   <option value="MIL (Nepali)">MIL (Nepali)</option>
   <option value="Political Science">Political Science</option>
   <option value="Education">Education</option>
   <option value="Economics">Economics</option>
   <option value="History">History</option>
   <option value="Geography">Geography</option>
   <option value="Sociology">Sociology</option>
   <option value="Logic & Philosophy">Logic & Philosophy</option>
   <option value="Advance Assamese">Advance Assamese</option>
   <option value="Advance Bengali">Advance Bengali</option>
   <option value="Physics">Physics</option>
   <option value="Chemistry">Chemistry</option>
   <option value="Biology">Biology</option>
   <option value="Mathematics">Mathematics</option>
   <option value="Computer Science & Applications">Computer Science & Applications</option>
   <option value="Accountancy">Accountancy</option>
   <option value="Business Studies">Business Studies</option>
   <option value="Banking">Banking</option>
   <option value="Statistics">Statistics</option>
   </select></td>

   </tr>

   <tr>

   <td>Elective-2</td>

     <td><select name="Elective-2">

   <option value="-1" selected>Select..</option>

   <option value="MIL (Hindi)">MIL (Hindi)</option>
   <option value="MIL (Bengali)">MIL (Bengali)</option>
   <option value="MIL (Nepali)">MIL (Nepali)</option>
   <option value="Political Science">Political Science</option>
   <option value="Education">Education</option>
   <option value="Economics">Economics</option>
   <option value="History">History</option>
   <option value="Geography">Geography</option>
   <option value="Sociology">Sociology</option>
   <option value="Logic & Philosophy">Logic & Philosophy</option>
   <option value="Advance Assamese">Advance Assamese</option>
   <option value="Advance Bengali">Advance Bengali</option>
   <option value="Physics">Physics</option>
   <option value="Chemistry">Chemistry</option>
   <option value="Biology">Biology</option>
   <option value="Mathematics">Mathematics</option>
   <option value="Computer Science & Applications">Computer Science & Applications</option>
   <option value="Accountancy">Accountancy</option>
   <option value="Business Studies">Business Studies</option>
   <option value="Banking">Banking</option>
   <option value="Statistics">Statistics</option>
   </select></td>

   </tr>

   <tr>

   <td>Elective-3</td>

     <td><select name="Elective-3">

   <option value="-1" selected>Select..</option>

   <option value="MIL (Hindi)">MIL (Hindi)</option>
   <option value="MIL (Bengali)">MIL (Bengali)</option>
   <option value="MIL (Nepali)">MIL (Nepali)</option>
   <option value="Political Science">Political Science</option>
   <option value="Education">Education</option>
   <option value="Economics">Economics</option>
   <option value="History">History</option>
   <option value="Geography">Geography</option>
   <option value="Sociology">Sociology</option>
   <option value="Logic & Philosophy">Logic & Philosophy</option>
   <option value="Advance Assamese">Advance Assamese</option>
   <option value="Advance Bengali">Advance Bengali</option>
   <option value="Physics">Physics</option>
   <option value="Chemistry">Chemistry</option>
   <option value="Biology">Biology</option>
   <option value="Mathematics">Mathematics</option>
   <option value="Computer Science & Applications">Computer Science & Applications</option>
   <option value="Accountancy">Accountancy</option>
   <option value="Business Studies">Business Studies</option>
   <option value="Banking">Banking</option>
   <option value="Statistics">Statistics</option>
   </select></td>

   </tr>

   <tr>

   <td>Elective-4</td>

     <td><select name="Elective-4">

   <option value="-1" selected>Select..</option>

   <option value="MIL (Hindi)">MIL (Hindi)</option>
   <option value="MIL (Bengali)">MIL (Bengali)</option>
   <option value="MIL (Nepali)">MIL (Nepali)</option>
   <option value="Political Science">Political Science</option>
   <option value="Education">Education</option>
   <option value="Economics">Economics</option>
   <option value="History">History</option>
   <option value="Geography">Geography</option>
   <option value="Sociology">Sociology</option>
   <option value="Logic & Philosophy">Logic & Philosophy</option>
   <option value="Advance Assamese">Advance Assamese</option>
   <option value="Advance Bengali">Advance Bengali</option>
   <option value="Physics">Physics</option>
   <option value="Chemistry">Chemistry</option>
   <option value="Biology">Biology</option>
   <option value="Mathematics">Mathematics</option>
   <option value="Computer Science & Applications">Computer Science & Applications</option>
   <option value="Accountancy">Accountancy</option>
   <option value="Business Studies">Business Studies</option>
   <option value="Banking">Banking</option>
   <option value="Statistics">Statistics</option>
   </select></td>

   </tr>

   <tr>

   <td>Compulsory</td>

     <td><select name="Compulsory">

     <option value="-1" selected>Select..</option>

       <option value="Environmental Education">Environmental Education</option>

     </select></td>

     </tr>



   <tr>

     <td>Institute</td>

     <td><select name="Institute">

     <option value="-1" selected>Select..</option>

     <option value="Institute-1">Institute-1</option>

     <option value="Institute-2">Institute-2</option>

     <option value="Institute-3">Institute-3</option>

     </select></td>

   </tr>

   <tr>

     <td>State</td>

     <td><select Name="State">

     <option value="-1" selected>select..</option>

     <option value="New Delhi">NEW DELHI</option>

     <option value="Mumbai">MUMBAI</option>

     <option value="Goa">GOA</option>

     <option value="Bihar">BIHAR</option>

     </select></td>

     </tr>

     <tr>

     <td>PinCode</td>

     <td><input type="text" name="pincode" id="pincode" size="30"></td>

   </tr>

     <tr>

     <td>EmailId</td>

     <td><input type="text" name="emailid" id="emailid" size="30"></td>

     </tr>

   <tr>

     <td>DOB</td>

     <td><input type="text" name="dob" id="dob" size="30"></td>

     </tr>

   <tr>

     <td>MobileNo</td>

     <td><input type="text" name="mobileno" id="mobileno" size="30"></td>

     </tr>

     <tr>

     <td><input type="reset"></td>


     <td colspan="2"><input type="submit" value="Submit Form" /></td>

     </tr>

     </table>

     </form>

     </body>

     </html>

以下是html代码的输出

enter image description here

我已经根据Bill的建议创建了一个.php作为proces.php文件,但是内容不是写但只给出了,,,,,,,这里是我的php脚本

 <?php
 $fieldA = $_POST["textnames"];
 $fieldB = $_POST["fathername"];
 $fieldC = $_POST["paddress"];
 $fieldD = $_POST["personaladdress"];
 $fieldE = $_POST["sex"];
 $fieldF = $_POST["Stream"];
 $fieldG = $_POST["Core-1"];
 $fieldH = $_POST["Core-2"];
 $fieldI = $_POST["Elective-1"];
 $fieldJ = $_POST["Elective-2"];
 $fieldK = $_POST["Elective-3"];
 $fieldL = $_POST["Elective-4"];
 $fieldM = $_POST["Compulsory"];

 $keys = array($fieldA,$fieldB,$fieldC,$fieldD,$fieldE,$fieldF,$fieldG,$fieldH,$fieldI,$fieldJ,$fieldK,$fieldL,$fieldM); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
 $csv_line = array();
 foreach($keys as $key){
     array_push($csv_line,'' . $_GET[$key]);
 }
 $fname = 'data.csv'; //NAME OF THE FILE
 $csv_line = implode(',',$csv_line);
 if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;}
 $fcon = fopen($fname,'a');
 $fcontent = $csv_line;
 fwrite($fcon,$fcontent);
 fclose($fcon);
 ?>

并将我原来的html代码从<form action="#" name="StudentRegistration" onSubmit="return(validate());">编辑为<form name="StudentRegistration" action="proces1.php" method="post">,所以这是我最新的html编码

 <html>

   <head>

   <script type="text/javascript" src="validate.js"></script>

   </head>

   <body>

   <form name="StudentRegistration" action="proces.php" method="post">


 <table cellpadding="2" width="20%" bgcolor="99FFFF" align="center"

   cellspacing="2">

 <tr>

   <td colspan=2>

   <center><font size=4><b>Student Registration Form</b></font></center>

   </td>

   </tr>

 <tr>

   <td>Name</td>

   <td><input type=text name=textnames id="textname" size="30"></td>

   </tr>

 <tr>

   <td>Father Name</td>

   <td><input type="text" name="fathername" id="fathername"

   size="30"></td>

   </tr>

   <tr>

   <td>Postal Address</td>

   <td><input type="text" name="paddress" id="paddress" size="30"></td>

   </tr>

 <tr>

   <td>Personal Address</td>

   <td><input type="text" name="personaladdress"

   id="personaladdress" size="30"></td>

   </tr>

 <tr>

   <td>Sex</td>

   <td><input type="radio" name="sex" value="male" size="10">Male

   <input type="radio" name="sex" value="Female" size="10">Female</td>

   </tr>

 <tr>

   <td>Stream</td>

   <td><select name="Stream">

   <option value="-1" selected>Select..</option>

   <option value="ARTS">ARTS</option>

   <option value="COMMERCE">COMMERCE</option>

   <option value="SCIENCE">SCIENCE</option>

   </select></td>

   </tr>

 <tr>

   <td>Core-1</td>

   <td><select name="Core-1">

   <option value="-1" selected>Select..</option>

   <option value="English">English</option>

   </select></td>

   </tr>
 <tr>

   <td>Core-2</td>

   <td><select name="Core-2">

   <option value="-1" selected>Select..</option>

   <option value="Alternative English">Alternative English</option>

   </select></td>

   </tr>

 <tr>

 <td>Elective-1</td>

   <td><select name="Elective-1">

   <option value="-1" selected>Select..</option>

   <option value="MIL (Hindi)">MIL (Hindi)</option>
   <option value="MIL (Bengali)">MIL (Bengali)</option>
   <option value="MIL (Nepali)">MIL (Nepali)</option>
   <option value="Political Science">Political Science</option>
   <option value="Education">Education</option>
   <option value="Economics">Economics</option>
   <option value="History">History</option>
   <option value="Geography">Geography</option>
   <option value="Sociology">Sociology</option>
   <option value="Logic & Philosophy">Logic & Philosophy</option>
   <option value="Advance Assamese">Advance Assamese</option>
   <option value="Advance Bengali">Advance Bengali</option>
   <option value="Physics">Physics</option>
   <option value="Chemistry">Chemistry</option>
   <option value="Biology">Biology</option>
   <option value="Mathematics">Mathematics</option>
   <option value="Computer Science & Applications">Computer Science & Applications</option>
   <option value="Accountancy">Accountancy</option>
   <option value="Business Studies">Business Studies</option>
   <option value="Banking">Banking</option>
   <option value="Statistics">Statistics</option>
   </select></td>

   </tr>

   <tr>

   <td>Elective-2</td>

     <td><select name="Elective-2">

   <option value="-1" selected>Select..</option>

   <option value="MIL (Hindi)">MIL (Hindi)</option>
   <option value="MIL (Bengali)">MIL (Bengali)</option>
   <option value="MIL (Nepali)">MIL (Nepali)</option>
   <option value="Political Science">Political Science</option>
   <option value="Education">Education</option>
   <option value="Economics">Economics</option>
   <option value="History">History</option>
   <option value="Geography">Geography</option>
   <option value="Sociology">Sociology</option>
   <option value="Logic & Philosophy">Logic & Philosophy</option>
   <option value="Advance Assamese">Advance Assamese</option>
   <option value="Advance Bengali">Advance Bengali</option>
   <option value="Physics">Physics</option>
   <option value="Chemistry">Chemistry</option>
   <option value="Biology">Biology</option>
   <option value="Mathematics">Mathematics</option>
   <option value="Computer Science & Applications">Computer Science & Applications</option>
   <option value="Accountancy">Accountancy</option>
   <option value="Business Studies">Business Studies</option>
   <option value="Banking">Banking</option>
   <option value="Statistics">Statistics</option>
   </select></td>

   </tr>

   <tr>

   <td>Elective-3</td>

     <td><select name="Elective-3">

   <option value="-1" selected>Select..</option>

   <option value="MIL (Hindi)">MIL (Hindi)</option>
   <option value="MIL (Bengali)">MIL (Bengali)</option>
   <option value="MIL (Nepali)">MIL (Nepali)</option>
   <option value="Political Science">Political Science</option>
   <option value="Education">Education</option>
   <option value="Economics">Economics</option>
   <option value="History">History</option>
   <option value="Geography">Geography</option>
   <option value="Sociology">Sociology</option>
   <option value="Logic & Philosophy">Logic & Philosophy</option>
   <option value="Advance Assamese">Advance Assamese</option>
   <option value="Advance Bengali">Advance Bengali</option>
   <option value="Physics">Physics</option>
   <option value="Chemistry">Chemistry</option>
   <option value="Biology">Biology</option>
   <option value="Mathematics">Mathematics</option>
   <option value="Computer Science & Applications">Computer Science & Applications</option>
   <option value="Accountancy">Accountancy</option>
   <option value="Business Studies">Business Studies</option>
   <option value="Banking">Banking</option>
   <option value="Statistics">Statistics</option>
   </select></td>

   </tr>

   <tr>

   <td>Elective-4</td>

     <td><select name="Elective-4">

   <option value="-1" selected>Select..</option>

   <option value="MIL (Hindi)">MIL (Hindi)</option>
   <option value="MIL (Bengali)">MIL (Bengali)</option>
   <option value="MIL (Nepali)">MIL (Nepali)</option>
   <option value="Political Science">Political Science</option>
   <option value="Education">Education</option>
   <option value="Economics">Economics</option>
   <option value="History">History</option>
   <option value="Geography">Geography</option>
   <option value="Sociology">Sociology</option>
   <option value="Logic & Philosophy">Logic & Philosophy</option>
   <option value="Advance Assamese">Advance Assamese</option>
   <option value="Advance Bengali">Advance Bengali</option>
   <option value="Physics">Physics</option>
   <option value="Chemistry">Chemistry</option>
   <option value="Biology">Biology</option>
   <option value="Mathematics">Mathematics</option>
   <option value="Computer Science & Applications">Computer Science & Applications</option>
   <option value="Accountancy">Accountancy</option>
   <option value="Business Studies">Business Studies</option>
   <option value="Banking">Banking</option>
   <option value="Statistics">Statistics</option>
   </select></td>

   </tr>

   <tr>

   <td>Compulsory</td>

     <td><select name="Compulsory">

     <option value="-1" selected>Select..</option>

       <option value="Environmental Education">Environmental Education</option>

     </select></td>

     </tr>



   <tr>

     <td>Institute</td>

     <td><select name="Institute">

     <option value="-1" selected>Select..</option>

     <option value="Institute-1">Institute-1</option>

     <option value="Institute-2">Institute-2</option>

     <option value="Institute-3">Institute-3</option>

     </select></td>

   </tr>

   <tr>

     <td>State</td>

     <td><select Name="State">

     <option value="-1" selected>select..</option>

     <option value="New Delhi">NEW DELHI</option>

     <option value="Mumbai">MUMBAI</option>

     <option value="Goa">GOA</option>

     <option value="Bihar">BIHAR</option>

     </select></td>

     </tr>

     <tr>

     <td>PinCode</td>

     <td><input type="text" name="pincode" id="pincode" size="30"></td>

   </tr>

     <tr>

     <td>EmailId</td>

     <td><input type="text" name="emailid" id="emailid" size="30"></td>

     </tr>

   <tr>

     <td>DOB</td>

     <td><input type="text" name="dob" id="dob" size="30"></td>

     </tr>

   <tr>

     <td>MobileNo</td>

     <td><input type="text" name="mobileno" id="mobileno" size="30"></td>

     </tr>

     <tr>

     <td><input type="reset"></td>


     <td colspan="2"><input type="submit" value="Submit Form" /></td>

     </tr>

     </table>

     </form>

     </body>

     </html>

2 个答案:

答案 0 :(得分:1)

这篇文章可以帮到你需要的地方......

How to export html form to csv file

那里的答案很好地解释了剧本的运作方式。

编辑: 你需要创建一个PHP文件并在点击提交时传递数据FORM应该看起来像这样注意POST和.php文件。

<form action="NAMEOFFILE.php" method="post">

NAMEOFFILE.PHP将在附加的链接中包含表单数据和导出到CSV脚本。如果不确定如何传递数据,这是一个简单的解释。 https://www.tutorialrepublic.com/php-tutorial/php-get-and-post.php

<?php
$fieldA = $_POST["textnames"];
$keys = array('id_number','first_name'); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
$csv_line = array();
foreach($keys as $key){
    array_push($csv_line,'' . $_GET[$key]);
}
$fname = 'file_to_write_to.csv'; //NAME OF THE FILE
$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);
?>

该文件将以名称“file_to_write_to.csv”保存在您的服务器上

答案 1 :(得分:0)

感谢大家现在我通过更改从$csv_line = array();$csv_line = $keys;以及从if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;}if(!file_exists($fname)){$csv_line = $csv_line."\r\n" ;}的行来编辑proces.php脚本后终于得到了输出。我最后的.php文件

 <?php
 $fieldA = $_POST["textnames"];
 $fieldB = $_POST["fathername"];
 $fieldC = $_POST["paddress"];
 $fieldD = $_POST["personaladdress"];
 $fieldE = $_POST["sex"];
 $fieldF = $_POST["Stream"];
 $fieldG = $_POST["Core-1"];
 $fieldH = $_POST["Core-2"];
 $fieldI = $_POST["Elective-1"];
 $fieldJ = $_POST["Elective-2"];
 $fieldK = $_POST["Elective-3"];
 $fieldL = $_POST["Elective-4"];
 $fieldM = $_POST["Compulsory"];

 $keys = array($fieldA,$fieldB,$fieldC,$fieldD,$fieldE,$fieldF,$fieldG,$fieldH,$fieldI,$fieldJ,$fieldK,$fieldL,$fieldM); //THIS IS WHERE YOU PUT THE FORM ELEMENTS ex: array('$fieldA','$fieldB',etc)
 $csv_line = $keys;
 foreach( $keys as $key ){
     array_push($csv_line,'' . $_GET[$key]);

 }
 $fname = 'data.csv'; //NAME OF THE FILE
 $csv_line = implode(',',$csv_line);
 if(!file_exists($fname)){$csv_line = $csv_line."\r\n" ;}
 $fcon = fopen($fname,'a');
 $fcontent = $csv_line;
 fwrite($fcon,$csv_line);
 fclose($fcon);
 ?>