0

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.

1
  • You results page is checking for +, -, etc. as an operation but it should be checking for 1, 2, etc. Commented Jun 29, 2019 at 9:01

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your quick response! However, I've tried it and the result page still shows nothing so far, I have also tried clearing cache but no update on the problem so far... I feel like I'm missing a large chunk of something I should be writing beforehand?
You might try checking your POST variables with var_dump($_POST); or echo "<pre>".print_r($_POST,true)."</pre>"; Also, I'm not aware of an input type "integer", but there is a "number" type, but that shouldn't matter much.
It just displays "NULL", I think it has something to do with the first three lines of the php code?
But $input1 has a value? That's odd. How about echo "<pre>".print_r($_REQUEST,true)."</pre>";
Right now it shows a different error, I added var_dump($operation); below the php code and placed values in input 1 and input 2 so basically trying to add 2 integers together and it displays string(0) ""
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.