我的理解是您可以使用PHP将值从表单POST到.txt文件。我尝试在下面的代码中这样做,但我有几个问题。为清楚起见,我将我的代码标记并注释为以下几个部分以供参考。
我有一个基本的表单验证,用于检查表单的字段是否为空。如果它们不是空的,则允许表单提交表单数据&POST / POST。我已经包含了一个IF语句,如果表单已经完成,它会尝试将表单数据写入.txt文件,如果字段为空,我的表单验证似乎不起作用。如果字段已满,则代码告诉我它将数据写入文件。在检查我的XAMPP服务器的工作目录但是,我找不到所述文件。事实上,我无法在任何地方的电脑上找到它。
我的问题是这样的:(1)为什么增加我的IF(标有// ********如果下面的声明是一个突破我的代码的声明****)声明写表单输入到文本文件打破我的表单验证? (2)这些信息写在哪里?
感谢您的帮助,我们非常感谢您。如果你能花时间更深入地解释为什么出现问题,那么这也是值得赞赏的,因为这是我第一次尝试这一点。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<h2>MAKE A CLIENT ENTRY</h2>
<label for="client">Client:</label>
<input type="text" placeholder = "Enter Client Name" name="client"> *
<!-- while typing in client, search firm-client database for this client. Once a client is entered, populate any associated matters in the matter input area. If there is no associated client, offer the option to create new client by dropdown. Store client name entered in temporary variable. Reveal newClientForm. -->
<label for="matter">Matter:</label>
<input type="text" placeholder = "Enter Matter Name" name="matter"> *
<!--while typing in client-matter, search associated client-matters for an existing matter. If there is no associated client-matter, offer the option to create a new client-matter by dropdown. Store client-matter name entere in temporary variable. Reveal newMatterFrom for client entered-->
<!--restrict inputs to a date -->
<label for="date">Date:</label>
<input type="text" placeholder = "Enter Date" name="date"> *
<!--Restrict to double values to the tenth of an hour -->
<label for="time">Time:</label>
<input type="text" placeholder ="Time to nearest tenth hour" name="time"> *
<!--no character restrictions -->
<label for="note">Note:</label>
<textarea name="note" placeholder ="Enter Any Notes" rows="4" cols="40"></textarea>
<input type="submit" name="submit" value="Submit" class="submitbutton">
<p class="required">* Required Fields </p>
<!-- this is the error div where errors will
be displayed.
must figure out why some errors display sometimes and
and not all errors
-->
<!-- on submit, search firm-client database for this client. if client exists search client-matter database for matter. if client-matter exists, get matter entries. add entry to entries. replace client-matter in database -->
</form>
<?php
//form validation for general entry form
// define variables and set to empty values
$clientErr = $matterErr = $dateErr = $timeErr = $noteErr= "";
$client = $matter = $date = $time = $note = "";
$complete = true;
//on post, check to see if variable is empty. if not empty
//parse it and assign value back to variable name
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["client"])) {
$clientErr = "*A client name is required. ";
$complete = false;
echo "1";
}else {
$client = test_input($_POST["client"]);
}
if (empty($_POST["matter"])) {
$matterErr = "*A matter name is required. ";
$complete = false;
echo "2";
}else {
$matter = test_input($_POST["matter"]);
}
if (empty($_POST["date"])) {
$dateErr = "*A date is required. ";
$complete = false;
echo "3";
}else {
$date = test_input($_POST["date"]);
}
if (empty($_POST["time"])) {
$timeErr = "*A time entry is required. ";
$complete = false;
echo "4";
}else {
$time = test_input($_POST["time"]);
}
if (empty($_POST["note"])) {
$noteErr = "*A note is required. ";
$complete = false;
echo "5";
} else {
$note = test_input($_POST["note"]);
}
//if $complete is true after all the if satements above run then
//append each entry together with a '-' separating the data and with
// '\n' at the end.
if ($complete){
//appending of the input data
$data = $_POST["client"].'-'.$_POST["matter"].'-'.$_POST["date"].'-'.$_POST["time"].'-'.$_POST["note"]."\n";
//create the mydata.txt file in the current directory with the
//value of $data. If the file exists already, appened to it
//with the value of $data.
$ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
//if $ret and false are identical (same value and type)
//then output that there was an error writing the file.
//else echo that the bytes were written to the file.
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div class="errorDiv">
<?php
echo $clientErr;
echo $matterErr;
echo $dateErr;
echo $timeErr;
echo $noteErr;
?>
</div>
<div class="inputDisplay">
<?php
//have to change this to an array to display the input correctly and not
//just the last of the inputs submitted
echo "<h2>Your Input:</h2>";
echo $client;
echo "<br>";
echo $matter;
echo "<br>";
echo $date;
echo "<br>";
echo $time;
echo "<br>";
echo $note;
?>
</div>
</body>
</html>
答案 0 :(得分:0)
在$complete = false;
的每个地方,在每个地方添加不同号码的echo "1";
。现在再试一次,你会看到哪个先前的条件导致$ complete变为false。
或者,如果你想在div中看到te错误,请删除以下3行:
else {
die('no post data to process');
}