【发布时间】:2016-01-31 15:26:12
【问题描述】:
据我了解,您可以使用 PHP 将表单中的值 POST 到 .txt 文件中。我试图在下面的代码中这样做,但我有几个问题。为清楚起见,我将我的代码标记和注释到下面的几个部分中以供参考。
我有一个基本的表单验证,用于检查我的表单字段是否为空。如果它们不为空,则允许表单“提交”/发布表单数据。我包含了一个 IF 语句,如果表单完整,它会尝试将表单数据写入 .txt 文件,如果字段为空,我的表单验证似乎不起作用。如果字段已满,代码会告诉我它已将数据写入文件。然而,在检查我的 XAMPP 服务器的工作目录后,我找不到该文件。事实上,我在任何地方的电脑上都找不到它。
我的问题是这样的:(1)为什么添加我的 IF(标有 //********下面的 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>
【问题讨论】:
-
可能是通过删除不相关的html?你也可以在“添加我的 IF 语句”的代码中做一些注释,因为不清楚你的意思是哪一个
-
我会努力这样做,但我认为显示网格结构的 3 行 html 不会太繁琐以至于需要删减。除非你说还有更多。在那种情况下,我只是对 HTML 不够精通,无法知道哪些其他代码是多余的。关于 IF 声明,我可以看到需要澄清的地方。我将尝试将其标记为红色,并感谢您指出这一点。
-
tmp目录是否存在/你有权限吗?试试
$ret = file_put_contents('mydata.txt', $data, FILE_APPEND | LOCK_EX);看看它是否显示。 -
您发布的代码的 2/3 似乎无关紧要,尤其是第一个“页面”,没有多少会向下滚动
-
@PanayiotisSpanos on OSX
tmp目录存在!在终端中尝试cd /tmp。否则,通过echo $TMPDIR(在终端中)。
标签: php html forms validation