这是我在服务器的文件夹中编写的PHP脚本:
<?php
$answer = $_POST['ans'];
if ($answer == "yes.") {
$testo = "qwe\r\n";
$documento ="/other/yes.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
else {
$testo = "qwe\r\n";
$documento ="/other/no.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
fclose($identificatore);
header('http://mk7vrlist.altervista.org/other/poll.html');
?>
此代码从无线电组获取答案,如果答案为Yes
,则会生成一个名为yes.txt
的文件。否则它会生成一个名为no.txt
的文件。这是图像。
这是HTML代码:
<form name="mainform" action="poll.php" method="POST">
<br />
<input type="radio" name="yes" value="yes">Yes, I like it.<br>
<br />
<input type="radio" name="yes" value="no" onclick="apri();">No, I want the old layout.
</fieldset>
//other code...
<input type="submit" value="Send" />
</td>
</table>
</form>
单击“发送”按钮时,脚本不会在服务器上保存任何内容。你知道为什么吗?
答案 0 :(得分:3)
一个问题是你使用的是$ _POST ['和'],但你的实际单选按钮被称为'是'。
表单中还有一个stray close fieldset标记。
答案 1 :(得分:1)
出于调试目的,在尝试fopen时应该使用die语句。
$fh = fopen($documento, 'a') or die('Could not append to file');
如果启用了错误报告,您可以随时使用 error_get_last()来更详细地了解失败的原因。
答案 2 :(得分:1)
试试这个: / project_folder -poll.php - 其他(创建具有写权限的其他文件夹)。
poll.php
<?php
$base = __DIR__; //if your php version dont work __DIR__ try: dirname(__FILE__);
if ($answer == "yes.") {
$testo = "qwe\r\n";
$documento =$base."/other/yes.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
else {
$testo = "qwe\r\n";
$documento =$base."/other/no.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
fclose($identificatore);
header('http://mk7vrlist.altervista.org/other/poll.html');
答案 3 :(得分:1)
首先,您的单选按钮应调用name="ans"
而不是name="yes"
您的if ($answer == "yes.")
不应该包含点。
您的标题格式不正确:
header('http://mk7vrlist.altervista.org/other/poll.html');
应为:
header('Location: http://mk7vrlist.altervista.org/other/poll.html');
headers
的更多信息:http://php.net/manual/en/function.header.php 此:
<input type="radio" name="yes" value="no" onclick="apri();">
onclick="apri();
不属于那里。如果您要使用此功能,则会与submit
按钮一起使用。
此$testo = "qwe\r\n";
可能需要重新格式化为$testo = "qwe" . "\n";
Linux和Windows使用\r
<form name="mainform" action="poll.php" method="POST">
<br />
<input type="radio" name="ans" value="yes">Yes, I like it.<br>
<br />
<input type="radio" name="ans" value="no">No, I want the old layout.
</fieldset>
<input type="submit" value="Send" />
</td>
</table>
</form>
<?php
$answer = $_POST['ans'];
if ($answer == "yes") {
$testo = "YES\n";
$documento ="yes.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
else {
$testo = "NO\n";
$documento ="no.txt";
$identificatore = fopen($documento, "a");
fwrite($identificatore, $testo) ;
}
fclose($identificatore);
header('Location: http://mk7vrlist.altervista.org/other/poll.html');
// echo "ok done";
?>
EDIT(作为计数器方法可选)
您可能还希望将答案用作计数器,而不是追加最终会变得非常大的数据。
注意:您必须先创建两个文件,其中包含数字0
(零)。
yes_count.txt
和no_count.txt
以下是这种方式的测试示例:
<?php
$answer = $_POST['ans'];
if ($answer == "yes") {
$fr_yes = fopen("yes_count.txt", "r");
$text_yes = fread($fr_yes, filesize("yes_count.txt"));
$fw = fopen("yes_count.txt", "w");
$text_yes++;
fwrite($fw, $text_yes);
// will echo the counter but you can use header to redirect after
echo $text_yes;
// header('Location: http://mk7vrlist.altervista.org/other/poll.html');
}
else {
$fr_no = fopen("no_count.txt", "r");
$text_no = fread($fr_no, filesize("no_count.txt"));
$fw = fopen("no_count.txt", "w");
$text_no++;
fwrite($fw, $text_no);
// will echo the counter but you can use header to redirect after
echo $text_no;
// header('Location: http://mk7vrlist.altervista.org/other/poll.html');
}
?>