2012-05-21 69 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