【问题标题】:How do I display the answer of my PHP and HTML program?如何显示我的 PHP 和 HTML 程序的答案?
【发布时间】:2016-05-21 12:15:14
【问题描述】:

我有我的 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)) 而不是标准开关来允许它显示。

【问题讨论】:

标签: php html web


【解决方案1】:

错误在于您的选择。你有

&lt;option name="subtraction"&gt;Subtraction&lt;/option&gt;

它们应该有值,而不是名称:

&lt;option value="subtraction"&gt;Subtraction&lt;/option&gt;

【讨论】:

  • 我也试过这个!当我在众多解释开关的网站中进行筛选时,我被告知不要使用值和使用名称。然而,这并没有奏效。
  • name 不是选项的有效属性。建议您使用它的网站是错误的。至于您的代码,我已经在本地对其进行了测试,并且按照上述更改的预期工作......2 added with 2 is 4.00
  • 那么它一定是我的 CSS 或页面在我的 iframe 中的显示方式,因为当我单击计算按钮时,只显示返回按钮。
  • 在你的 switch 中,添加一个默认条件,看看你是否仍然没有输出。
【解决方案2】:

使用switch (strtolower($arithmetic))。你的算术后值有上升的情况......

【讨论】:

  • 啊,就是这样!谢谢!
  • 这只是因为您错误地使用了name 而不是value,正如我在回答中所说的那样。没有值的选项将使用文本作为值(请注意,name 属性绝对没有任何作用)。 如果你按照我的描述解决了这个问题,代码就会正常工作!
【解决方案3】:

试试:

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

【讨论】:

  • 我更改了我的代码;仍然没有工作,但仍然没有错误。感谢发帖!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 2022-06-13
  • 2018-06-28
  • 1970-01-01
  • 2011-12-21
相关资源
最近更新 更多