Jump to content

simple summation program


ahsn

Recommended Posts

I am a newbie in php and learning it eagerly.

 

Anyway, I would like to make a program which will take two numbers from users and then show the result at the same page.

 

Could anyone please show me the coding to do that?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.