2016-02-10 57 views
1

我有我的代碼爲我的PHP:如何顯示我的PHP和HTML程序的答案嗎?

<?php 
$num1= $_REQUEST["number1"]; 
$num2= $_REQUEST["number2"]; 
$arithmetic = $_POST['arithmetic']; 

switch ($arithmetic) { 
case "addition": 
    $answer = $num1+$num2; 
    echo "<p>$num1 added with $num2 is " . number_format($answer,2) . "</p>"; 
    break; 

case "subtraction": 
    $answer = $num1-$num2; 
    echo "<p>$num2 subtracted from $num1 is " . number_format($answer,2) .  "</p>"; 
    break; 

case "multiplication": 
    $answer = $num1*$num2; 
    echo "<p>$num1 multiplied by $num2 is " . number_format($answer,2) . "</p>"; 
    break; 

case "division": 
    $answer = $num1/$num2; 
    echo "<p>$num1 divided by $num2 is " . number_format($answer,2) . "</p>"; 
    break; 

case "modulus": 
    $answer = $num1%$num2; 
    echo "<p>The remainder of $num1 divided by $num2 is " . number_format($answer,2) . "</p>"; 
    break; 
} 
?> 

<br> 
<form action="calcD.html" target="iframeMain"> 
    <input type="submit" value="go back"> 
</form> 

<?php 
    echo "<HR>"; 
    highlight_file("calcD.php"); 
?> 

和我爲我的HTML代碼:

<html> 
<head> 
    <link href="reset.css" rel="stylesheet" type="text/css"> 
    <link href="css3.css" rel="stylesheet" type="text/css"> 
    <link href='https://fonts.googleapis.com/css?family=Scada:400,700' rel='stylesheet' type='text/css'> 
</head> 
<body> 
    <h2>Dropdown Calculator</h2> 
    <form action="calcD.php" method="POST" target="iframeMain" autocomplete="off"> 
    <p>Number 1:<input type="text" name="number1"></p> 
    <p>Number 2:<input type="text" name="number2"></p> 
    <h3>Type of Arithmetic</h3> 
    <select name="arithmetic"> 
    <option name="addition">Addition</option> 
    <option name="subtraction">Subtraction</option> 
    <option name="multiplication">Multiplication</option> 
    <option name="division">Division</option> 
    <option name="modulus">Modulus</option> 
    </select> 
    <input type="submit" name="compute" value="Compute"> 
    </form> 
    <a href="assignment3.html" target="iframeMain">Back to Menu</a> 
</body> 
</html> 

但是當我嘗試使用我的節目,回答不顯示。我檢查了多個網站,並且我的HTML和我的PHP沒有錯誤,但是我的答案沒有顯示。我甚至嘗試過爲每個程序使用If和Elseif語句。有沒有一個鏈接,我失蹤了他們?

編輯:我需要使用switch (strtolower($arithmetic))而不是標準的開關,以允許其顯示。

+0

我看不到「選擇」:http://www.w3schools.com/tags/att_option_selected.asp –

+0

我加了這個,仍然沒有運氣顯示我的答案。 – Justin

+0

然後更新你的問題。 –

回答

0

使用switch (strtolower($arithmetic))。你已經在你的算術崗位值case ...

+0

這是它!謝謝! – Justin

+1

這僅僅是因爲你沒有正確使用'name',而不是'value',正如我在答覆中說。沒有值的選項將使用文本作爲值(請注意name屬性完全沒有)。 **如果您按照我所述修復了這個問題,代碼將正常工作!** – rjdown

2

的錯誤是在你的選擇。你有

<option name="subtraction">Subtraction</option>

他們應該有值,而不是名稱:

<option value="subtraction">Subtraction</option>

+0

我也試過這個!當我通過大量的網站解釋開關時,我被告知不要使用價值和使用名稱。儘管如此,這並沒有奏效。 – Justin

+0

'name'不是選項的有效屬性。建議你使用它的網站是錯誤的。至於你的代碼,我已在本地測試,它按預期工作與改變上述....'2加2是4.00' – rjdown

+0

那麼它一定是我的CSS或頁面是如何因爲當顯示在我的IFRAME我單擊計算按鈕,只顯示返回的按鈕。 – Justin

0

嘗試:

$num1= $_POST["number1"]; 
$num2= $_POST["number2"]; 
+0

我改變了我的代碼;仍然沒有工作,但仍然沒有錯誤。感謝發佈! – Justin