为什么post action打印代码作为打印字符串而不是结果?(php)


Why would post action print code as print strings and not results? (php)

这个是学校的,所以如果你不想帮我-我提前警告你。

我在一个html页面中有这段代码,它引用了一个名为dieRoller.php的页面:

<form method="post" action="dieRoller.php">
<label>Input how many sides your die has:</label>
<input type="numberInput" id="numberInput" name="numberInput" /><br />
<input type="submit" value="submit" name="submit" />
</form>

这是dierler .php:

<?php
$numberInput = filter_input(INPUT_POST, "numberInput");
$answerNum = rand(1,numberInput);
print " You've rolled a: " $answerNum;
?>

这可能是地球上最愚蠢的问题,但我不明白为什么,当我提交这个表单时,我的结果实际上是dieRoller.php的文本文件,而不是打印输出?我搜遍了。我不明白。

提前感谢您的帮助

你有以下错误

您忘记在rand函数中为php变量添加$符号

$answerNum = rand(1,numberInput);

$answerNum = rand(1,$numberInput);

你的dieRoller应该是这样的:

<?php
if(isset($_POST['numberInput'])){
echo rand(1,$_POST['numberInput']);
}
?>

下面更正了代码中的两个拼写错误,经过测试后更正的代码可以正常工作:


$answerNum = rand(1,$numberInput);
print " You've rolled a: " .$answerNum;