Andrew M
Andrew M

Reputation: 3

PHP Calculator program not displaying results

I am trying to make a simple PHP calculator program however the results of the form are not being displayed onto the next page.

Would greatly appreciate any help I can get.

This is the code for the calculator form:

<body>
        <center>
            <div class="container">
                <form action="hasil_calculator.php" method="post"> <!--Action: Sent to "cek_grade.php" Method: The data will be displayed by "post"-->
                   Kalkulator:<br>
                   <input type="integer" name="input1" placeholder="Integer"> <!--The name of the numbers inputted are "nilai"-->
                   <select id ="operation" name="operation">
                        <option value="">Operation</option>
                        <option value="1">X</option>
                        <option value="2">/</option>
                        <option value="3">+</option>
                        <option value="4">-</option>
                   </select>
                   <input type="integer" name="input2" placeholder="Integer">
                   <br><br>
                   <input type="submit" id="submit" name="submit" value="Submit">
                </form> <!--All the data above should get sent to the page called "cek_grade.php"-->
            </div>
       </center>
    </body>

This is the code for the results:

<?php

    $input1 = $_POST['input1'];
    $input2 = $_POST['input2'];
    $operation = $_POST['operation'];

    if($operation == '+') {
        echo $input1 + $input2;
    }

    elseif($operation == '-') {
        echo $input1 - $input2;
    }

    elseif($operation == 'X') {
        echo $input1 * $input2;
    }

    elseif($operation == '/') {
        echo $input1 / $input2;
    }

?>

So far I have tried: - Getting rid of any unnecessary spaces - Using $ on echo "$input1 + $input2" (I think putting $ will display both the integers instead of an answer? I'm not sure since nothing is displaying) - Using echo "input1 + input2" instead of the above so it shows a result instead of both integers but then again nothing shows.

Upvotes: 0

Views: 221

Answers (2)

Samir Patel
Samir Patel

Reputation: 197

You have written this line when from closed. Means you want to go to "cek_grade.php" page when form submitted.

Then why u set the action as <!--All the data above should get sent to the page called "cek_grade.php"--> <form action="hasil_calculator.php" method="post">

Specify the page name in which u has written PHP Code

Upvotes: 0

showdev
showdev

Reputation: 29168

Based on your <option> values, the value for $operation will be "1", "2", "3", or "4".

if($operation == '1') {
    echo $input1 * $input2;
}

elseif($operation == '2') {
    echo $input1 / $input2;
}

elseif($operation == '3') {
    echo $input1 + $input2;
}

elseif($operation == '4') {
    echo $input1 - $input2;
}

For future debugging, one tip is to check the values you're getting, like this:

var_dump($operation);

Upvotes: 1

Related Questions