首先,您的两个单选按钮都应该被调用name="ans"
,而不是name="yes"
你if ($answer == "yes.")
不应该有一个点。
而且您的标题格式不正确:
header('http://mk7vrlist.altervista.org/other/poll.html');
应该读作:
header('Location: http://mk7vrlist.altervista.org/other/poll.html');
这个:
<input type="radio" name="yes" value="no" onclick="apri();">
onclick="apri();
不属于那里。如果你要使用这个,将与一个submit
按钮一起使用。
这$testo = "qwe\r\n";
可能需要重新格式化,因为$testo = "qwe" . "\n";
Linux 和 Windows 使用不同的反应\r
HTML 表单(重新格式化)
<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 处理程序
<?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";
?>
编辑(可选作为计数器方法)
您可能还希望将您的答案用作计数器,而不是附加最终可能会变得非常大的数据。
注意:您必须首先创建两个文件,其中包含数字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');
}
?>