我想知道如何使用PHP将HTMLform中的信息发送到文本文件。我当前的代码没有向文本文件添加任何内容。 HTML和PHP文件如下。希望有人可以帮助我,如果需要更多信息,请告诉我,我会在评论中向您提供信息。
HTML
<form action="results.php" method="post">
<table border="1">
<tr>
<th colspan="7">Survey (CSC443)</th>
</tr>
<tr>
<td rowspan="2">#</td>
<td rowspan="2">Question</td>
<td>Strongly</td>
<td>Somewhat</td>
<td></td>
<td>Somewhat</td>
<td>Strongly</td>
</tr>
<tr align="center">
<td colspan="2">Disagree</td>
<td>Neutral</td>
<td colspan="2">Agree</td>
</tr>
<tr>
<td>1</td>
<td>This course helped me develop problem-solving skills.</td>
<td align="center"><input type="radio" name="q1" value="1"></td>
<td align="center"><input type="radio" name="q1" value="2"></td>
<td align="center"><input type="radio" name="q1" value="3" checked></td>
<td align="center"><input type="radio" name="q1" value="4"></td>
<td align="center"><input type="radio" name="q1" value="5"></td>
</tr>
<tr>
<td>2</td>
<td>This course was very challenging.</td>
<td align="center"><input type="radio" name="q2" value="1"></td>
<td align="center"><input type="radio" name="q2" value="2"></td>
<td align="center"><input type="radio" name="q2" value="3" checked></td>
<td align="center"><input type="radio" name="q2" value="4"></td>
<td align="center"><input type="radio" name="q2" value="5"></td>
</tr>
<tr>
<td>3</td>
<td>My programming ability increased after aking this course.</td>
<td align="center"><input type="radio" name="q3" value="1"></td>
<td align="center"><input type="radio" name="q3" value="2"></td>
<td align="center"><input type="radio" name="q3" value="3" checked></td>
<td align="center"><input type="radio" name="q3" value="4"></td>
<td align="center"><input type="radio" name="q3" value="5"></td>
</tr>
<tr>
<td>4</td>
<td>I will list 'Javascript' on my resume.</td>
<td align="center"><input type="radio" name="q4" value="1"></td>
<td align="center"><input type="radio" name="q4" value="2"></td>
<td align="center"><input type="radio" name="q4" value="3" checked></td>
<td align="center"><input type="radio" name="q4" value="4"></td>
<td align="center"><input type="radio" name="q4" value="5"></td>
</tr>
<tr align="center">
<td colspan="2"></td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td colspan="7" rowspan="2" align="center"><input type="submit" value="Submit"> <input type="reset" value="Reset"></td>
</tr>
</table>
</form>
PHP
if(isset($_POST['q1'] && isset($_POST['q2'] && isset($_POST['q3'] && isset($_POST['q4'])
{
$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
$q4 = $_POST['q4'];
$text = $q1 . "," . $q2 . "," . $q3 . "," . $q4 . "\n";
$ret = file_put_contents('/results.txt', $text, FILE_APPEND | LOCK_EX);
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');
}
答案 0 :(得分:0)
你的result.php中有一些小错误,这里是固定的php代码
<?php
if(isset($_POST['q1']) && isset($_POST['q2']) && isset($_POST['q3']) && isset($_POST['q4']))
{
$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
$q4 = $_POST['q4'];
$text = $q1 . "," . $q2 . "," . $q3 . "," . $q4 . "\n";
$ret = file_put_contents('results.txt', $text, FILE_APPEND | LOCK_EX);
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');
}
?>
答案 1 :(得分:0)
问题在于这一行,你有几个)
个缩短的括号。
if(isset($_POST['q1'] && isset($_POST['q2'] && isset($_POST['q3'] && isset($_POST['q4'])
----) ----) ----) ----)
将其替换为:
if(isset($_POST['q1']) && isset($_POST['q2']) && isset($_POST['q3']) && isset($_POST['q4']))
另外,可能还有其他问题。我(成功)使用以下方法对此进行了测试:
$ret = file_put_contents('results.txt', $text, FILE_APPEND | LOCK_EX);
/
results.txt
如果您需要设置文件,则将文件权限设置为0644
比0777
更安全。
重写 :(从与文本文件相同的文件夹中运行代码)
如果您的文件位于执行文件夹的一个级别,请使用'../results.txt'
,具体取决于执行代码和文本文件的位置。
<?php
if(isset($_POST['q1']) && isset($_POST['q2']) && isset($_POST['q3']) && isset($_POST['q4']))
{
$q1 = $_POST['q1'];
$q2 = $_POST['q2'];
$q3 = $_POST['q3'];
$q4 = $_POST['q4'];
$text = $q1 . "," . $q2 . "," . $q3 . "," . $q4 . "\n";
$ret = file_put_contents('results.txt', $text, FILE_APPEND | LOCK_EX);
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');
}
?>