2012-05-21 111 views
0

我想提交一个表单并获取写入到CSV文件,并存储在主机的信息。并且每个新表单都应该在csv中添加一个新行。另外,可以说我有几个用户可以加入的组--A,b,c,d,e。我如何做到这一点,如果用户选择加入两个或三个组(1,2和3组),它将把这个信息存储为123在同一个单元格中。以下是我的HTML代码:PHP表单提交到CSV文件

<!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

你缺少一个右支架

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

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

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

为未定义如果不贴形式。

+0

我得到它的工作,我如何让它显示出与返回按钮成功的消息,还我怎么让它所以,如果用户选择两个或多个组,它显示在csv文件1个单元所有的人? – krest

+0

我想你可以有这种方法的并发问题,如果多个用户同时提交 – jujule