Jump to content


Photo

Build a calculator in PHP

calculator php html

  • Please log in to reply
4 replies to this topic

#1 Codin

Codin

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 20 June 2013 - 03:34 PM

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



#2 Barand

Barand

    Sen . ( ile || sei )

  • Gurus
  • 13,885 posts
  • LocationCheshire, UK

Posted 20 June 2013 - 03:47 PM

And your question is ... ?


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns

 


#3 Codin

Codin

    Newbie

  • New Members
  • Pip
  • 2 posts

Posted 20 June 2013 - 04:57 PM

And your question is ... ?

My question is this is not working can anyone see where the mistake is I am not getting any errors but it does not echo out the results. Thanks



#4 boompa

boompa

    Advanced Member

  • Members
  • PipPipPip
  • 211 posts
  • LocationMassachusetts

Posted 20 June 2013 - 05:15 PM

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();?>


#5 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 20 June 2013 - 05:16 PM

Add this to the page you're submitting to, preferably at the top. It'll show you why things aren't working:
var_dump ($_REQUEST);

Keeping it simple.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com