2012-03-23 43 views
0

我完全新的PHP。並一直遵循教程。我需要完成一個調用php函數的表單,它將表單的輸入寫入文件。應該很容易嗎?我已經在很多方面嘗試了這一點,這是我所得到的結果。然而,由於某種原因,它寫作三次,這意味着三個條目的估算,那麼第三次之後的所有內容都被忽略。 test.txt是我寫的文件。PHP的形式寫入TXT

<?php 
    $email= $_POST['email']; 
    $myFile = "test.txt"; 
    $fh = fopen($myFile, 'a') or die("can't open file"); 
    $stringData = "$email\n"; 
    fwrite($fh, $stringData); 
    fclose($fh); 
?> 

這裏是我用來使用php的表單。

<div id="login-box"> 
    <form name="form" method="post" action="<?php $_SERVER['PHP_SELF'];?>"> 
     <div class="text-field"> 
    <input name="email" id="email" type="text"> 
</div> 
<input id="login" type="submit" value="Submit"> 
     </form> 
     </div> 
+1

你怎麼知道「之後的第三一切都被忽略」? – 2012-03-23 06:07:09

+0

我的網站開發人員正在向我彙報。不過,我個人在我的表單中收到「無法打開文件」。所以這個表格不適合我。 – zetologos 2012-03-23 23:56:02

+0

-1雖然,發佈最初的問題,即使沒有實際的錯誤信息。 – 2012-03-24 04:17:34

回答

0

我已經測試你的代碼並沒有不管你是mentioning.The代碼插入三個以上record.So請隨身攜帶的任何問題......

+0

也許它是我的html格式。 – zetologos 2012-03-23 23:56:14

-1

也許你提交表單的3倍或按下瀏覽器面板上的重新加載按鈕? 你能發佈你的html嗎?

,並嘗試這個辦法:

<?php 

    if($_POST['email']) 
    { 
     $email= $_POST['email']; 
     $myFile = "test.txt"; 
     $fh = fopen($myFile, 'a') or die("can't open file"); 
     $stringData = "$email\n"; 
     fwrite($fh, $stringData); 
     fclose($fh); 
    } 

?> 
+0

我收到「無法打開文件」錯誤 – zetologos 2012-03-24 00:00:16

0

嘗試使用這個例子

<?php 
$email= $_POST['email']; 
$myFile = "test.txt"; 

// First, let's make sure that the file exists and is writable. 
if (is_writable($myFile)) { 

    // In our example we're opening $ myFile in the "append". 
    if (!$handle = fopen($myFile, 'a')) { 
     echo "Can't open ($myFile)"; 
     exit; 
    } 

    if (fwrite($handle, $email) === FALSE) { 
     echo "Can't wtire to file ($myFile)"; 
     exit; 
    } 

    echo "OK. Content ($email) written to file ($myFile)"; 
    fclose($handle); 

} else { 
    echo "File $myFile not available for writing."; 
} 
?> 

您也可以使用此代碼簡單地調試你的PHP代碼:

echo <something_what_you_need>; die; 

也許叫你的代碼多次,你看不到它。

0

你的代碼應該工作,但你在表單的action屬性中寫道<?而不是<?php

+0

固定的,但一直沒有解決的問題。 – zetologos 2012-03-24 00:38:08

0

在你的PHP代碼

ini_set('display_errors',1); 
error_reporting(E_ALL); 

頂部添加這兩條線,看到了真正的錯誤信息,而不是你傻「無法打開」,但解釋,什麼是肯定會出錯。
如果你不能得到它的意思你自己,它張貼在這裏,準確和完整