我有以下代码用于PHP测验。
当我将表单数据通过
但是当我将表单数据通过
我遇到了以下错误。
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(($_POST['q_1'] == '') || ($_POST['q_2'] == '') || ($_POST['q_3'] == '') || ($_POST['q_4'] == '') || ($_POST['q_5'] == '')) {
$nameError = 'Please choose an option';
$hasError = true;
}
else {
for ($i=1; $i<=$types; $i+=1)
{
$nowval[$i] = 0;
}
for ($i=1; $i<=$questions; $i+=1)
{
$qvar = "q_$i";
//echo $qvar;
tally($_POST[$qvar]);// LINE THAT CAUSES ERROR
}
$dominant = 1;
$domval = $nowval[1];
for ($i=2; $i<=$types; $i+=1)
{
if ($domval < $nowval[$i])
{
$dominant = $i;
$domval = $nowval[$i];
}
}
function tally ($question) // TALLY FUNCTION
{
global $nowval;
$nowval[$question]++;
}
if (is_file("$quiz.rsl"))
{
$fp = fopen("$quiz.rsl", 'r');
$line = fgets($fp, 1024);
fclose($fp);
$people = explode("|", $line);
$people[$dominant-1] += 1;
$timestaken = 0;
foreach($people as $tally)
{
settype($tally, 'integer');
$timestaken += $tally;
}
$fp = fopen("$quiz.rsl", 'w');
for($i=0; $i<$types; $i++)
{
fwrite($fp, $people[$i]."|");
}
fclose($fp);
}
else
{
for($i=0; $i<$types; $i++)
{
$people[] = 0;
}
$people[$dominant-1] += 1;
$timestaken = 1;
$fp = fopen("$quiz.rsl", 'w');
for($i=0; $i<$types; $i++)
{
fwrite($fp, $people[$i]."|");
}
fclose($fp);
}
$percentage = ($people[$dominant-1] / $timestaken) * 100;
$dec=2;
$format="%.$dec" . "f";
$number=sprintf($format,$percentage);
$percentage=strtok($number,".");
$dc=strtok(".");
if ($dec!=0)
{
$percentage = "$percentage" . ".$dc";
}
$emailSent = true;
}//else
}//main if
当我将表单数据通过
POST
方法提交到另一个文件时,如果以上代码写在一个单独的文件中,一切都正常。但是当我将表单数据通过
POST
方法提交到当前页面时,出现了问题。Fatal error: Call to undefined function tally()
我不理解这个问题的根本原因。
当我尝试使用
$this->tally($_POST[$qvar]);
我遇到了以下错误。
Fatal error: Using $this when not in object context