如何显示PHP和HTML程序的答案?

时间:2016-02-10 22:23:29

标签: php html web

我有我的PHP代码:

 <?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))代替标准开关才能显示。

3 个答案:

答案 0 :(得分:2)

错误在您的选项中。

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

他们应该有价值观,而不是名字:

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

答案 1 :(得分:0)

尝试:

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

答案 2 :(得分:0)

使用switch (strtolower($arithmetic))。你的算术岗位值中有案例......