【问题标题】:Echo not displaying my result/answerEcho 不显示我的结果/答案
【发布时间】:2016-10-28 09:01:49
【问题描述】:

我正在使用 php 7.0、apache2、mysql ver 14.14 发行版 5.6.17 和 phpmyadmin 5.7

由于某种原因,我的 php 文件不会显示变量 $result 的值。每当我尝试使用“回声”时,它都不会显示。甚至没有另一个变量(总和)。

我的 html 文件有以下代码:

<!doctype html>
<html lang="en-us">
<head>
<title>calculation form</title>
<meta charset="utf-8">
</head>


<body>

<form method="post" action="calculate.php">
<p>Value 1: <input type="text" name="val1" size="10"></p>
<p>Value 2: <input type="text" name="val2" size="10"></p>
<p>Calculation:<br>
<input type="radio" name="calc" value="add"> add<br>
<input type="radio" name="calc" value="subtract"> subtract<br>
<input type="radio" name="calc" value="multiply"> multiply<br>
<input type="radio" name="calc" value="divide"> divide
</p>
<p><input type="submit" name="submit" value="Calculate"></p>
</form>
</body>
</html> 

我的php代码如下:

<?php

$sum = 0;

if(($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == ""))
{
    header("Location: calculate_form.html");
    exit;
}

if($_POST[calc] == "add")
{
    $result = $_POST[val1] + $_POST[val2];
}

if($_POST[calc] == "subtract")
{
    $result = $_POST[val1] - $_POST[val2];
}

if($_POST[calc] == "multiply")
{
    $result = $_POST[val1] - $_POST[val2];
}

if ($_POST[calc] == "divide") {
    $result = $_POST[val1] / $_POST[val2];
}

?>



<?php

echo $result;
echo $sum;

?>

【问题讨论】:

    标签: php lamp php-7


    【解决方案1】:

    你必须在 if 条件中回显,否则你必须全局设置变量

    <?php
    
        $sum = 0;
    
        if(($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == ""))
        {
            header("Location: calculate_form.html");
            exit;
        }
    
        if($_POST['calc'] == "add")
        {
            $result = $_POST['val1'] + $_POST['val2'];
            echo $result;
        }
    
        if($_POST['calc'] == "subtract")
        {
            $result = $_POST['val1'] - $_POST['val2'];
            echo $result;
        }
    
        if($_POST['calc'] == "multiply")
        {
            $result = $_POST['val1'] - $_POST['val2'];
            echo $result;
        }
    
        if ($_POST['calc'] == "divide") {
            $result = $_POST['val1'] / $_POST['val2'];
            echo $result;
        }
    
        ?>
    

    或者这样设置

     if($_POST[calc] == "add")
        {
            $result = $_POST[val1] + $_POST[val2];
            $sum= $result;
        }
    

    你在$_POST[val1]也有错误

    应该是$_POST['val1']

    更新示例

    <?php
    
        $sum = 0;
    $_POST['val1']=5;
    $_POST['val2']=10;
    $_POST['calc']='add';
        if(($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == ""))
        {
            header("Location: calculate_form.html");
            exit;
        }
    
        if($_POST['calc'] == "add")
        {
           $sum = $_POST['val1'] + $_POST['val2'];
    
        }
    
        if($_POST['calc'] == "subtract")
        {
          $sum = $_POST['val1'] - $_POST['val2'];
    
        }
    
        if($_POST['calc'] == "multiply")
        {
          $sum = $_POST['val1'] - $_POST['val2'];
    
        }
    
        if ($_POST['calc'] == "divide") {
           $sum = $_POST['val1'] / $_POST['val2'];
    
        }
    echo $sum;
        ?>
    

    【讨论】:

    • 我试过了,但没有用。它不会显示任何内容。
    • 请看 $_POST[val1] 并且应该用单引号引用 $_POST['val1']
    • 由于某种原因,当我将 echo 添加到所有条件语句中时,它起作用了。非常感谢兄弟。我还尝试在开始时初始化变量。 Smdh为什么我不早点理解……太尴尬了。再次感谢 :)
    • 我会的。在我接受答案之前,它给了我 5 分钟。 :)
    • @JosephBourne.okay
    猜你喜欢
    • 1970-01-01
    • 2019-08-25
    • 1970-01-01
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    相关资源
    最近更新 更多