Jump to content

Archived

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

ahsn

simple summation program

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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

@davidannis, thank you very much for the codes.

Share this post


Link to post
Share on other sites
<!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>

Share this post


Link to post
Share on other sites

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