0

我想提交一个表格并将信息写入 CSV 文件并存储在主机中。每个新表单都应该在 csv 中添加一个新行。另外,假设我有几个用户可以加入的组 - A、b、c、d、e。如果用户选择加入 2 个或三个组(1,2 和 3rd 组),我该如何做到这一点,它将将此信息作为 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 都在同一个文件夹中。

4

1 回答 1

1

你缺少一个右括号

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

你应该启用error_reporting(E_ALL)它会显示错误和使用

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

如果表格未发布,则为未定义。

于 2012-05-21T01:43:51.347 回答