0

这是我在服务器文件夹中编写的 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>

当我单击“发送”按钮时,脚本不会在服务器上保存任何内容。你知道为什么吗?

4

4 回答 4

3

一个问题是您正在使用 $_POST['ans'] 但您的实际单选按钮称为“是”。

您的表单中还有一个杂散的关闭字段集标记。

于 2013-09-01T16:38:35.613 回答
1

出于调试目的,您应该在尝试打开 fopen 时使用 die 语句。

$fh = fopen($documento, 'a') or die('Could not append to file');

如果错误报告已打开,您始终可以使用error_get_last()来获得有关失败原因的更详细描述。

于 2013-09-01T16:45:34.243 回答
1

您正在尝试写入/(根目录)php 用户对该文件夹有权限吗?

试试这个:/project_folder -poll.php -other(创建其他具有写权限的文件夹)。

投票.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');
于 2013-09-01T16:47:18.643 回答
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');

这个:

<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.txtno_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');

}

?>
于 2013-09-01T16:47:41.300 回答