2013-03-23 142 views
0

这个学校,所以如果你不想帮助我,我提前警告你。为什么会发布操作将打印代码打印为打印字符串而不是结果? (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> 

这是dieRoller.php:

<?php 

$numberInput = filter_input(INPUT_POST, "numberInput"); 
$answerNum = rand(1,numberInput); 

print " You've rolled a: " $answerNum; 

?> 

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

非常感谢您的帮助。

+2

这是在安装了php的服务器上吗? – Musa 2013-03-23 04:23:44

+0

这是在一个测试安装的xampp .... – 2013-03-23 04:25:34

+0

你确定其他PHP文件运行? PHP是否加载了Apache?另外,'numberInput'不是[有效的'input'类型](http://www.whatwg.org/specs/web-apps/current-work/multipage/the-input-element.html#attr-input-类型)。 – ajp15243 2013-03-23 04:26:05

回答

4

您有以下错误。

忘记在rand函数中添加$ sign for php变量。

更换

$answerNum = rand(1,numberInput); 

$answerNum = rand(1,$numberInput); 
+0

谢谢你的Dipesh。我看到了你的评论并修复了它! :) – 2013-03-23 04:29:40

+0

@orchd欢迎兄弟。 – 2013-03-23 04:30:45

+0

@orchd你现在应该接受答案。 – 2013-03-23 04:44:19

0

你dieRoller应该是这样的:

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

当然,这比我拥有的要优雅得多。谢谢。 – 2013-03-23 04:30:12

0

代码中的两个错别字修正下方修正合作测试时出现问题:


$answerNum = rand(1,$numberInput); 

print " You've rolled a: " .$answerNum;