Page 1 of 1

Calculator

Posted: Wed Apr 29, 2009 11:54 pm
by hoku_2000
I am working on making a calculator, by combining html and php. The only thing that shows up is my html and my php doesnt work. I know it works as two separate files, but I am trying to get everything for this calculator to work in one file. The problem that I have when I test it, is that the results dont come out when I click the calculate button.

Code: Select all

<html>
 
<head>
<title>Calculator</title>
    <script type="text/javascript">
    function formReset()
{
document.getElementById("calculatorForm").reset()
}
</script>
</head>
 
<body bgcolor="maroon">
 
<form id="calculatorForm">
 
 
<h1><center>Calculator</center></h1>
 
<b><p>Please enter two (2) numbers where asked, choose your function (add, subtract, multiply, or divide), then click on the calculate button to get your calculations.</p></b>
   
    <p align="center"><b>Enter the first number :</b><br/><input type="text" name="num1" size="20"><br>
    <p align="center"><b>Enter the second number : </b><br/><input type="text" name="num2"     size="20"></p>
</p>
    <p align="center"><select name = "function">
      <option value="+">Add [+]</option>
      <option value="-">Subtract[-]</option>
      <option value="*">Multiple[*]</option>
      <option value="/">Divide[/]</option>
    </select></p>
 
 
    <p align="center"><input type="submit" value="Calculate" name="calculate"></p>
    <p align="center"><input type="submit" value="clear" name="clear"></p>
 
 
<?php
$num1 = $_POST['num1'];
$num2 = $_POST['num2'];
$func = $_POST['function'];
if (!$num1="" || !$num2="")
{
die("Value Cannot Be Null");
exit();
}
else
{
switch ($func) {
    case "+":
       $result = $_POST[num1] + $_POST[num2];
    case "-":
       $result = $_POST[num1] - $_POST[num2];
    case "*":
        $result = $_POST[num1] * $_POST[num2];
    case "/":
        $result = $_POST[num1] / $_POST[num2];
}
echo "Calculation result:" $result;
}
?>
</form>
</body>
</html>

Re: Calculator

Posted: Wed Apr 29, 2009 11:58 pm
by requinix

Code: Select all

<form id="calculatorForm">
Where does the form go? Does it use GET or POST?

Re: Calculator

Posted: Thu Apr 30, 2009 10:06 am
by hoku_2000
tasairis wrote:

Code: Select all

<form id="calculatorForm">
Where does the form go? Does it use GET or POST?
Its using POST.

Re: Calculator

Posted: Thu Apr 30, 2009 10:48 am
by susrisha

Code: Select all

 <form id="calculatorForm" method="POST"> 

Re: Calculator

Posted: Thu Apr 30, 2009 11:47 am
by hoku_2000
susrisha wrote:

Code: Select all

 <form id="calculatorForm" method="POST"> 
Would POST still work, even if I am using one form?

Re: Calculator

Posted: Thu Apr 30, 2009 2:00 pm
by requinix
hoku_2000 wrote:Would POST still work, even if I am using one form?
What are you talking about?

Yes, it will work. No idea why you think it wouldn't.

Re: Calculator

Posted: Thu Apr 30, 2009 2:31 pm
by hoku_2000
tasairis wrote:
hoku_2000 wrote:Would POST still work, even if I am using one form?
What are you talking about?

Yes, it will work. No idea why you think it wouldn't.
I was thinking that POST only works when your want to direct a user to another page.

Re: Calculator

Posted: Thu Apr 30, 2009 2:47 pm
by requinix
hoku_2000 wrote:
tasairis wrote:
hoku_2000 wrote:Would POST still work, even if I am using one form?
What are you talking about?

Yes, it will work. No idea why you think it wouldn't.
I was thinking that POST only works when your want to direct a user to another page.
Nope. It's just another method, like GET and PUT.

Re: Calculator

Posted: Thu Apr 30, 2009 3:53 pm
by hoku_2000
I got it working now. Thanks! :D