2016-03-14 57 views
0

我正在使用php導出.csv文件,但我正面臨着一些問題,例如我無法檢查複選框的值以及如何在頂行設置標題?如何在csv中導出選中的複選框值?

這裏是我的html

<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="intrest[]" type="checkbox" id="1" value="a" /></td> 
      <td>A</td> 
      <td><input name="intrest[]" type="checkbox" id="4" value="b" /></td> 
      <td>B</td> 
     </tr> 
     <tr> 
      <td><input name="intrest[]" type="checkbox" id="2" value="c" /></td> 
      <td>C</td> 
      <td><input name="intrest[]" type="checkbox" id="5" value="d" /></td> 
      <td>D</td> 
     </tr> 
     <tr> 
      <td><input name="intrest[]" 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> 

這裏是我的PHP代碼:

<?php 
    if (isset($_POST['intrest'])) { 
     $name = $_POST['intrest']; 

     echo "You chose the following color(s): <br>"; 
     foreach ($name as $color) { 
      $cvsData = $name; 
     } 
    } 
    $fn = $_POST['fn']; 
    $ln = $_POST['ln']; 
    $phone = $_POST['phone']; 
    $abc = (isset($_POST['1'])) ? $_POST['1'] : 'No'; 


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

    $cvsData = $phone . "," . $fn . "," . $ln . "," . $phone . "\n"; 

    $fp = fopen("formTest.csv", "a"); 
    if ($fp) { 
     fwrite($fp, $cvsData); 
     fclose($fp); 
    } 
?> 

我正在explaing我的代碼做什麼 Initally我沒有csv文件,所以當我點擊第一我的代碼創建一個新的csv文件並添加表單值。 當我點擊第二次我的代碼在新行中添加數據。 這就是我真正想要的。 但主要的問題是我不能在.csv文件中添加選中的複選框值,第二個問題是我如何添加標題在頂行中?
在此先感謝

回答

1

您不是真正構建所需的CSV數據,而是用新數據覆蓋相同的變量。看看下面的例子(不知道它是否會做你想要的,但它應該給你一個想法)。

<?php 
$cvsData = ""; //Initialize it as empty 
if(isset($_POST['intrest'])) { 
    $name = $_POST['intrest']; 

    echo "You chose the following color(s): <br>"; 
    foreach ($name as $color){ 
     $cvsData .= $color.",";  //Append the new color value 
    } 
} 
$fn = $_POST['fn']; 
$ln = $_POST['ln']; 
$phone = $_POST['phone']; 
$abc = (isset($_POST['1'])) ? $_POST['1'] : 'No'; 


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

$cvsData .= $phone . "," . $fn . "," . $ln . "," . $phone ."\n"; //Append the second line here 

//Add header here 
$exists = true; 
if (!is_readable("formTest.csv")) { 
    $exists = false; 
} 

$fp = fopen("formTest.csv","a"); 
if($fp){ 
    if (!$exists) { 
     fwrite("This is my awesome header\n"); 
    } 
fwrite($fp,$cvsData); 
fclose($fp); 
} 
?> 
+0

是工作的罰款,我喜@apokryfos但它在提交拋出錯誤** Array對C字符串轉換:\ XAMPP \ htdocs中\ 8行文件\ post.php中**,並在CSV返回數組我想如果我選擇多個intrest它應該在一列不是新的。 –

+0

進行了小幅更新。 – apokryfos

+0

嗨@apokryfos感謝它幫助我很多 –

相關問題