PHP表单提交到CSV文件

时间:2012-05-21 01:39:07

标签: php forms csv processor

我想提交一份表格并将信息写入CSV文件并存储在托管中。每个新表单都应该在csv中添加一个新行。另外,假设我有几个用户可以加入的组 - A,b,c,d,e。如果用户选择加入2个或3个组(1,2和3组),它将如何实现,它将在同一单元格中将此信息存储为123。以下是我的HTML代码:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Reg</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="post.php">
<table width="597" class="formatTblClass">
<tr>
<th colspan="4"></th>
</tr>
<tr>
<td width="99"><span>First Name</span></td>
<td width="217"><input class="" type="text" name="fn" id="fn" /></td>
<td width="99"><span>Last Name</span></td>
<td width="211"><input class="" name="ln" type="text" id="ln" /></td>
</tr>
<tr>
  <td>Phone</td>
  <td><input class="" type="text" name="phone" id="phone" /></td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td colspan="4">Check groups that you would like to receive updates about</td>
  </tr>
<tr>
  <td><input name="1" type="checkbox" id="1" value="a" /></td>
  <td>A</td>
  <td><input name="4" type="checkbox" id="4" value="b" /></td>
  <td>B</td>
</tr>
<tr>
  <td><input name="2" type="checkbox" id="2" value="c" /></td>
  <td>C</td>
  <td><input name="5" type="checkbox" id="5" value="d" /></td>
  <td>D</td>
</tr>
<tr>
  <td><input name="3" type="checkbox" id="3" value="e" /></td>
  <td>E</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td colspan="4">
  <div align="center">
  <input type="submit" name="Submit" id="Submit" value="Submit" />
  <input type="reset" name="Reset" id="button" value="Reset" />
  </div></td>
</tr>
</table>
</form>
</body>
</html>

这是我的post.php文件:

<?php
$fn = $_POST['fn'];
$ln = $_POST['ln'];
$phone = $_POST['phone'];
$1 = (isset($_POST['1'])) ? $_POST['1'] : 'No';
$2 = (isset($_POST['2'])) ? $_POST['2'] : 'No';
$3 = (isset($_POST['3'])) ? $_POST['3'] : 'No';
$4 = (isset($_POST['4'])) ? $_POST['4'] : 'No';
$5 = (isset($_POST['5'])) ? $_POST['5'] : 'No';

//validate

if(empty($fn) || empty($ln) || empty($phone)){//show the form
$message = 'Fill in areas in red!';
$aClass = 'errorClass';

//this is where the creating of the csv takes place
$cvsData = $phone . "," . $fn . "," . $ln . "," . $phone . "," . $1 ."\n";

$fp = fopen("formTest.csv","a"); // $fp is now the file pointer to file $filename

if($fp){
fwrite($fp,$cvsData); // Write information to the file
fclose($fp); // Close the file
?>

当我提交表单时,我会得到一个空白页面。怎么了? index.html,post.php和testForm.csv都在同一个文件夹中。

1 个答案:

答案 0 :(得分:1)

您错过了

的结束括号

if($fp){if(empty($fn) || empty($ln) || empty($phone)){

您应该启用error_reporting(E_ALL)它会显示错误并使用

$fn = $_POST['fn'];
$ln = $_POST['ln'];
$phone = $_POST['phone'];

如果表格未张贴,则为未定义。

相关问题