PHP-将输入数据保存到CSV文件中,并在另一页中显示整个文件

时间:2017-06-08 13:43:19

标签: php csv input

相当于标题。我想制作一个解决方案,保存html表单的输入数据,并在一个未连接的单独页面中显示整个csv文件。这是我正在使用的代码,它不保存输入数据也不显示csv文件。  输入数据页面:

<!DOCTYPE HTML>  
<html>
<head>
</head>
<body>

<h2>PHP Form Validation Example</h2>
<form method="post" name="test1" action="" onsubmit="required()">  
 Name: <input type="text" name="name">

<br><br>
 E-mail: <input type="text" name="email">

<br><br>
Website: <input type="text" name="website">

<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>

<br><br>
<input type="submit" name="submit" value="Submit">  
</form>
</body>
<?php

$name = $_POST['name'];
$email = $_POST['email'];
$web = $_POST['website'];
$comment = $_POST['comment'];

$csv = 
array($_POST['name'],$_POST['email'],$_POST['website'],$_POST['comment']);

$file = fopen('form.csv', 'a');

foreach ($csv as $line) 
{
  fputcsv($file,explode(',',$line));
}

fclose($file); 
?>
</html>

PHP:显示CSV文件

<!DOCTYPE html>
<html>

<?php
echo "</br >";
echo '<table style="width=100%">';

 // Print other student details here (minus the private stuff)
//The CSV file is read and printed on the page when using the 'r' value. 

$myfile = fopen("form.csv" , "r") or die ("Unable to open file");
while(!feof($myfile)) {
echo fgets($myfile) . "<br>"; 
}
fclose($myfile);

echo "</table>";
?>
<body>

</body>
</html>

0 个答案:

没有答案