Jump to content

simple summation program


ahsn

Recommended Posts

My son wrote this (with a little help from his dad):

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Calculator</title>
    </head>
    <body>
        <p>
        <form method="POST" action="Calculator.php">
            <input type="text" name="x"><br />
            <select name="operation">
                <option value="+">+</option>
                 <option value="-">minus</option>
                  <option value="*">*</option>
                   <option value="/">/</option>
                   <option value="sqrt">square root</option>
            </select><br />
            <input type="text" name="z">
            <input type="submit">
        </form>
        </p>
        <?php
        switch ($_POST["operation"]){
            case '+':
                $answer=$_POST['x']+$_POST['z'];
                break ;
            case '-':
                $answer=$_POST['x']-$_POST['z'];
                break ;
            case '*':
                $answer=$_POST['x']*$_POST['z'];
                break ;
            case '/':
                $answer=$_POST['x']/$_POST['z'];
                break ;
                case 'sqrt':
                $answer=sqrt($_POST['x']);
                break ;
        }
                        echo $answer;
        ?>
    </body>
</html>

any errors are mine.

Nitpick: The OP didn't specify that he wanted a calculator. He said he wanted to take two numbers and show the result.

 

The result of WHAT is yet to be defined.

Hopefully, if it's not one of the operations my 7 year old coded the OP can add another case to the switch statement. ;D  If not, perhaps my son can do it as a freelance job.

  • 3 weeks later...
<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>Calculator</title>

    </head>

    <body>

        <p>

        <form method="POST" action="">

          First Number : <input type="text" name="x"><br />

            Oprator :<select name="operation">

                <option value="+">+</option>

                 <option value="-">minus</option>

                  <option value="*">*</option>

                   <option value="/">/</option>

                   <option value="sqrt">square root</option>

            </select><br />

            Second Number :<input type="text" name="z">

            <input type="submit" name="result" value="result">

        </form>

        </p>

        <?php

if(isset($_POST['result']))

{

        switch ($_POST["operation"]){

            case '+':

                $answer="Sum is : " . ($_POST['x']+$_POST['z']);

                break ;

            case '-':

                $answer="Subtraction is : " .($_POST['x']-$_POST['z']);

                break ;

            case '*':

                $answer="Multiplication is : " .($_POST['x']*$_POST['z']);

                break ;

            case '/':

                $answer="Division is : " .($_POST['x']/$_POST['z']);

                break ;

                case 'sqrt':

                $answer= "Sqrt of first input is : " .(sqrt($_POST['x']));

                break ;

        }

         

               echo $answer;

}

        ?>

    </body>

</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.