2013-03-23 172 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;