Joseph Bourne Joseph Bourne - 1 month ago 6
PHP Question

Echo not displaying my result/answer

I'am using php 7.0, apache2, mysql ver 14.14 distribution 5.6.17, and phpmyadmin 5.7

My php file for some reason will not display the value for variable $result. Whenever I try to use "echo" it wont display it. Not even another variable (sum).

My html file has following code:

<!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>


My php code has the following:

<?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;

?>

Answer

you have to echo inside if condition or you have to set variable globally

<?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;
    }

    ?>

or else set like this

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

also yuo have error in $_POST[val1]

it should be $_POST['val1']

Updated with example

<?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")
    {
        $result = $_POST['val1'] + $_POST['val2'];
        $sum=$result ;
        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;
    }
echo $sum;
    ?>
Comments