2015-06-20 24 views
0

我有一個任務,要求從一個PHP表單的數據條目存儲在同一個Web服務器上的純文本文件。我創建了php表單頁面和純文本文件,但我不確定如何連接兩者。我搜索了互聯網,並嘗試了多種方式,現在約兩個小時,沒有骰子。數據條目也必須存儲在純文本文件中(第一人提交,第二人提交可以看到第一人的提交等)。如何將php表單數據以純文本文件存儲到Web服務器中?

我沒有添加任何我嘗試過的純文本文件的代碼,因爲它們都沒有工作,我想(理論上)簡化不必嘗試和修復代碼的過程。我知道純文本文件需要某種代碼才能從php表單中檢索代碼,但不確定在這一點上要做什麼。是的,我寫了純文本文件的權限可以通過FileZilla寫入。

這裏是我的PHP形式的代碼:

<!DOCTYPE HTML> 
<html> 
<head> 
<title>Roy Feedback Form Assignment 7</title> 
<style> 
.error {color: #FF0000;} 
</style> 
</head> 
<body> 

<?php 
// define variables and set to empty values 
$nameErr = $emailErr = $commentErr = $likesErr = $howErr = $rateErr = ""; 
$name = $email = $comment = $likes = $how = $rate = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
if (empty($_POST["name"])) { 
$nameErr = "Name is required"; 
} else { 
$name = test_input($_POST["name"]); 
} 


$email = test_input($_POST["email"]); 
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
$emailErr = "Invalid email format"; 
} 

if (empty($_POST["comment"])) { 
$commentErr = "Comments are required"; 
} else { 
$comment = test_input($_POST["comment"]); 
} 

if (empty($_POST["likes"])) { 
$likesErr = "Things you liked is required"; 
} else { 
$likes = test_input($_POST["likes"]); 
} 

if (empty($_POST["how"])) { 
$howErr = "How you got to our site is required"; 
} else { 
$how = test_input($_POST["how"]); 
} 

if (empty($_POST["rate"])) { 
$rateErr = "Rating our site is required"; 
} else { 
$rate = test_input($_POST["rate"]); 
} 
} 

function resetForm($form) { 
$form.find('input:text, input:password, input:file, select, textarea').val(''); 
$form.find('input:radio, input:checkbox') 
    .removeAttr('checked').removeAttr('selected'); 
} 

function test_input($data) { 
$data = trim($data); 
$data = stripslashes($data); 
$data = htmlspecialchars($data); 
return $data; 
} 
?> 

<h2>Roy Feedback Form Assignment 7</h2> 
<p><span class="error">* required field.</span></p> 
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 

Name: <input type="text" name="name"> 
<span class="error">* <?php echo $nameErr;?></span> 
<br><br> 

E-mail: <input type="text" name="email"> 
<span class="error">* <?php echo $emailErr;?></span> 
<br><br> 

Comment: <textarea name="comment" rows="5" cols="40"></textarea> 
<span class="error">* <?php echo $commentErr;?></span> 
<br><br> 

Things you liked: 
<input type="radio" name="likes" value="Site design">Site design 
<input type="radio" name="likes" value="Links">Links 
<input type="radio" name="likes" value="Ease of use">Ease of use 
<input type="radio" name="likes" value="Images">Images 
<input type="radio" name="likes" value="Source code">Source code 
<span class="error">* <?php echo $likesErr;?></span> 
<br><br> 

How you got to our site: 
<input type="radio" name="how" value="Search engine">Search engine 
<input type="radio" name="how" value="Links from another site">Links from another site 
<input type="radio" name="how" value="Deitel.com website">Deitel.com website 
<input type="radio" name="how" value="Reference from a book">Reference from a book 
<input type="radio" name="how" value="Other">Other 
<span class="error">* <?php echo $howErr;?></span> 
<br><br> 

Rate our site: 
<select name="rate"> 
<option value="">- Please Select -</option> 
<option value="1">1</option> 
<option value="2">2</option> 
<option value="3">3</option> 
<option value="4">4</option> 
<option value="5">5</option> 
<option value="6">6</option> 
<option value="7">7</option> 
<option value="8">8</option> 
<option value="9">9</option> 
<option value="10">10</option> 
</select> 
<span class="error">* <?php echo $rateErr;?></span> 
<br/><br/> 

<input type="submit" name="submit" value="Submit"> 
<input type="reset" value="Reset"> 

</form> 

</body> 
</html> 

而這裏的純文本文件代碼:

<!DOCTYPE html> 
<html> 
<head> 
    <title>Roy Feedback Results Assignment 7</title> 
</head> 
<body> 



</body> 
</html> 

更新1 所以我仍然有問題(很抱歉,我知道這是就像試圖教一個柚子教PHP一樣)。所以,當我使用下面的代碼沒有任何反應的純文本頁面上(如數據不存儲在哪裏,我告訴它是):

function file_write($data, $feedback_results_html_only){ 
if(is_readable($feedback_results_html_only)){ 
    if(is_string($data)){ 
     return file_put_contents($feedback_results_html_only, $data, FILE_APPEND | LOCK_EX);//this appends the new data to the file and locks it while doing so to prevent multiple access to thje file at the same time. 
    }//return an error message if the data isnt a string 
}//return an error message if the file doesnt exist or isnt readable 
} 

然而,當我使用下面的代碼就至少放置「約翰·史密斯」的名字到文件(這是我第一次真正得到它在一定程度上工作,編碼萬歲!):

<?php 
$file = 'feedback_results_html_only.php'; 
// Open the file to get existing content 
$current = file_get_contents($file); 
// Append a new person to the file 
$current .= "John Smith\n"; 
// Write the contents back to the file 
file_put_contents($file, $current); 
?> 

而且我知道,我沒有用「.PHP」上代碼的第一個示例(更新1中)中的「feedback_results_html_only」,因爲它創建了一個錯誤。我可以嘗試像第二個例子(在更新1),而不是使其工作?

$file = 'feedback_results_html_only.php' 
+0

退房http://php.net/manual/en/function.serialize.php和http://php.net/manual/en/function.file- put-contents.php也應該提到數據庫是這類事情的正確工具。 – rjdown

回答

0

您正在尋找file_put_contents()。只需收集數據並將其寫入您的文件。這裏是精緻代碼:

PS:你可以考慮先用PHP代碼開始:-)

<?php 
    // define variables and set to empty values 
    $nameErr = ''; 
    $emailErr = ''; 
    $commentErr = ''; 
    $likesErr = ''; 
    $howErr = ''; 
    $rateErr = ''; 
    $name = ''; 
    $email = ''; 
    $comment = ''; 
    $likes = ''; 
    $how = ''; 
    $rate = ''; 

if ($_SERVER["REQUEST_METHOD"] == "POST"){ 
    if (empty($_POST["name"])) { 
    $nameErr = "Name is required"; 
    } else { 
    $name = test_input($_POST["name"]); 
    } 


    $email = test_input($_POST["email"]); 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
    $emailErr = "Invalid email format"; 
    } 

    if (empty($_POST["comment"])) { 
    $commentErr = "Comments are required"; 
    } else { 
    $comment = test_input($_POST["comment"]); 
    } 

    if (empty($_POST["likes"])) { 
    $likesErr = "Things you liked is required"; 
    } else { 
    $likes = test_input($_POST["likes"]); 
    } 

    if (empty($_POST["how"])) { 
    $howErr = "How you got to our site is required"; 
    } else { 
    $how = test_input($_POST["how"]); 
    } 

    if (empty($_POST["rate"])) { 
    $rateErr = "Rating our site is required"; 
    } else { 
    $rate = test_input($_POST["rate"]); 
    } 
} 

function resetForm($form) { 
$form.find('input:text, input:password, input:file, select, textarea').val(''); 
$form.find('input:radio, input:checkbox') 
    .removeAttr('checked').removeAttr('selected'); 
} 

function test_input($data) { 
$data = trim($data); 
$data = stripslashes($data); 
$data = htmlspecialchars($data); 
return $data; 
} 



//concatenate the data, then format and validate it then use this function to write it to your plain text file 

function file_write($data, $pathtoplaintxtfile){ 
     if(is_string($data)){ 
      return file_put_contents($pathtoplaintxtfile, $data, FILE_APPEND | LOCK_EX);//this appends the new data to the file and locks it while doing so to prevent multiple access to thje file at the same time. 
     }//return an error message if the data isnt a string 
} 
    ?> 


    <!DOCTYPE HTML> 
    <html> 
     <head> 
      <title>Roy Feedback Form Assignment 7</title> 
      <style> 
       .error {color: #FF0000;} 
      </style> 
     </head> 
     <body> 
<h2>Roy Feedback Form Assignment 7</h2> 
<p><span class="error">* required field.</span></p> 
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 

Name: <input type="text" name="name"> 
<span class="error">* <?php echo $nameErr;?></span> 
<br><br> 

E-mail: <input type="text" name="email"> 
<span class="error">* <?php echo $emailErr;?></span> 
<br><br> 

Comment: <textarea name="comment" rows="5" cols="40"></textarea> 
<span class="error">* <?php echo $commentErr;?></span> 
<br><br> 

Things you liked: 
<input type="radio" name="likes" value="Site design">Site design 
<input type="radio" name="likes" value="Links">Links 
<input type="radio" name="likes" value="Ease of use">Ease of use 
<input type="radio" name="likes" value="Images">Images 
<input type="radio" name="likes" value="Source code">Source code 
<span class="error">* <?php echo $likesErr;?></span> 
<br><br> 

How you got to our site: 
<input type="radio" name="how" value="Search engine">Search engine 
<input type="radio" name="how" value="Links from another site">Links from another site 
<input type="radio" name="how" value="Deitel.com website">Deitel.com website 
<input type="radio" name="how" value="Reference from a book">Reference from a book 
<input type="radio" name="how" value="Other">Other 
<span class="error">* <?php echo $howErr;?></span> 
<br><br> 

Rate our site: 
<select name="rate"> 
<option value="">- Please Select -</option> 
<option value="1">1</option> 
<option value="2">2</option> 
<option value="3">3</option> 
<option value="4">4</option> 
<option value="5">5</option> 
<option value="6">6</option> 
<option value="7">7</option> 
<option value="8">8</option> 
<option value="9">9</option> 
<option value="10">10</option> 
</select> 
<span class="error">* <?php echo $rateErr;?></span> 
<br/><br/> 

<input type="submit" name="submit" value="Submit"> 
<input type="reset" value="Reset"> 

</form> 

</body> 
</html> 

編輯:file_put_contents()全自動創建一個文件,如果它不存在,刪除文件is_readable檢查。

編輯:用途 -

$data = $name.$email.$comment.$likes.$how.$rate. 
'This is just test data. If no other data is visible, then you didnt fill them out'; 
file_write($data, 'feedback_results_html_only.php') 
+0

感謝您的提示。當我使用你的代碼時,我收到了這個錯誤消息:>解析錯誤:語法錯誤,意外的'?',期待'('在/ home/a9156596/public_html/form_html_only.php在線71 –

+1

刪除行71 。我編輯了ans。請不要直接使用代碼(複製粘貼)....它只是一個想法,如何繼續。 –

+0

你是對的,謝謝你的澄清。你能檢查出我的更新1我的帖子?幫助我白金工業,你是我唯一的希望... –

0

使用file_put_contets

$relative_or_absolute_path = '../'; //relative folder up 
$ext = '.txt'; //file can be with no extension at all or even *.php 
$filename = 'plain_log'; 

$contents = ''; 
foreach($users as $user){ 
    $contents .= $user . '\n'; //a newline 
} 

//execute 

file_put_contents($relative_or_absolute_path.$filename.$ext, $contents, FILE_APPEND); 

//FILE_APPEND is an optional flag // otherwise it will rewrite 
+0

我需要在這段代碼中做的唯一調整是將我的文件路徑添加到「$ relative_or_absolute_path ...」右邊?此外,當我試圖說,「foreach($用戶爲$用戶)」錯誤「不知道這是爲什麼。我也不完全明白爲什麼它在那裏。 file_put_contents操作所需的代碼中的所有內容?這個例子非常簡單並且工作。我知道「FILE_APPEND」也需要在那裏。 –

+0

hm。這是我給的任意代碼。如果「用戶」是一個數組,則可以對其進行foreach。是的,file_put_contents是一個執行文件打開,文件寫入,以及處理關閉等功能。但是您必須設置dirrectory權限爲Writable(Google 4:chmod)使用termnal。 – animaacija

相關問題