I get some error in PHP, what can I do?

79 views Asked by At

So, I made a calculator in PHP, which writes the result and that how much time used the calculator.

The first time I get some error, but when I click any submit button, the program work perfectly. What can I do with first time errors?

The program:

<html>
<body>
    <div>
        <?php
            $using_calculator = $_POST['using_calculator'];
            if(isset($_POST['plus'])){
                    $x = $_POST['number_1'];
                    $y = $_POST['number_2'];
                    $z = $x+$y;
                    $using_calculator++;
            }else if(isset($_POST['minus'])){
                    $x = $_POST['number_1'];
                    $y = $_POST['number_2'];
                    $z = $x-$y;
                    $using_calculator++;
            }else if(isset($_POST['multiplication'])){
                    $x = $_POST['number_1'];
                    $y = $_POST['number_2'];
                    $z = $x*$y;
                    $using_calculator++;
            }else if(isset($_POST['division'])){
                    $x = $_POST['number_1'];
                    $y = $_POST['number_2'];
                    $z = $x/$y;
                    $using_calculator++;
            }
        ?>
        <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
            <p>
                First number: <input type="number" name="number_1"><br/>
                Second number: <input type="number" name="number_2"><br/><br/>
                <input type = "submit" name="plus" value="Plus">
                <input type = "submit" name="minus" value="Minus">
                <input type = "submit" name="multiplication" value="Multiplicate">
                <input type = "submit" name="division" value="Divide">
                <input type = "hidden" name="using_calculator" value="<?php echo $using_calculator; ?>">            
            </p>
        </form>
        <br>Result: <?php echo $z; ?>
        <br>The calculator was used <?php echo $using_calculator; ?> times
    </div>
</body>

I have to use hiddened input fields, but I can use functions and object. So what is the best way?

1

There are 1 answers

0
Unni Babu On

Its because first time variables(in your php code) is not isset. So it will display error. For example


 $using_calculator = $_POST['using_calculator']; // first time $_POST['using_calculator']  is not isset

So php will output error message first time. After submit this variables get initialized from POST. So to avoid this problem, modify the php code as shown below

 <?php

    if(isset($_POST['using_calculator']))
      {
                $using_calculator = $_POST['using_calculator'];
                if(isset($_POST['plus'])){
                        $x = $_POST['number_1'];
                        $y = $_POST['number_2'];
                        $z = $x+$y;
                        $using_calculator++;
                }else if(isset($_POST['minus'])){
                        $x = $_POST['number_1'];
                        $y = $_POST['number_2'];
                        $z = $x-$y;
                        $using_calculator++;
                }else if(isset($_POST['multiplication'])){
                        $x = $_POST['number_1'];
                        $y = $_POST['number_2'];
                        $z = $x*$y;
                        $using_calculator++;
                }else if(isset($_POST['division'])){
                        $x = $_POST['number_1'];
                        $y = $_POST['number_2'];
                        $z = $x/$y;
                        $using_calculator++;
                }
        }
        ?>