Jump to content

Build a calculator in PHP


Codin

Recommended Posts

I am trying to build a calculator in PHP which uses a certain formula to come up with the results. I have a form which collects the numbers and then posts to a calc.php page which calculates the answer. Here is my HTML code

 <!DOCTYPE html >

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Calculator</title>
</head>

<body>
    <form action="calc.php" method="POST">
        <input type="text" name="num1">
        <input type="text" name="num2">
        <input type="text" name="num3">
        <input type="submit" value="Calculate">

        <?php echo form_close();?>

    </form>

</body>
</html>

Here is my PHP but I just am missing something. 

<?php

function calculate($num1, $num2, $num3)
{ 
   $data = array() ;

   $data['c'] = $num1 / 50;
   $data['b'] = $num2 / 12;
   $data['s'] = $num3 / 5;
   $data['p'] = $data['c'] + $data['b'] - $data['s'];

    if($data['s'] > 4)
    {
        $data['s']= 4;
    }
    return $data ;
}

if (isset($_REQUEST['value'])) //Additional checks should be done tho.
{
    $num1 = $_REQUEST["num1"];
    $num2 = $_REQUEST["num2"];
    $num3 = $_REQUEST["num3"];

    $data = calculate($num1, $num2, $num3) ;
    echo "{$data['c']} + {$data['b']} - {$data['s']} = {$data['p']}" ;
}

Thank you for all help in advance

Link to comment
Share on other sites

You don't run your function and output anything unless a REQUEST variable named "value" is set. Do you have a form element with a name of "value"?

 

What's this doing in there?

 

<?php echo form_close();?>
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.