我有一個任務,要求從一個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'
退房http://php.net/manual/en/function.serialize.php和http://php.net/manual/en/function.file- put-contents.php也應該提到數據庫是這類事情的正確工具。 – rjdown