PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
hoku_2000
Forum Newbie
Posts: 8 Joined: Wed Apr 29, 2009 11:50 pm
Post
by hoku_2000 » Wed Apr 29, 2009 11:54 pm
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>
Last edited by
Benjamin on Thu Apr 30, 2009 12:49 am, edited 1 time in total.
Reason: Changed code type from text to php.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Wed Apr 29, 2009 11:58 pm
Where does the form go? Does it use GET or POST?
hoku_2000
Forum Newbie
Posts: 8 Joined: Wed Apr 29, 2009 11:50 pm
Post
by hoku_2000 » Thu Apr 30, 2009 10:06 am
tasairis wrote:
Where does the form go? Does it use GET or POST?
Its using POST.
susrisha
Forum Contributor
Posts: 439 Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India
Post
by susrisha » Thu Apr 30, 2009 10:48 am
Code: Select all
<form id="calculatorForm" method="POST">
hoku_2000
Forum Newbie
Posts: 8 Joined: Wed Apr 29, 2009 11:50 pm
Post
by hoku_2000 » Thu Apr 30, 2009 11:47 am
susrisha wrote: Code: Select all
<form id="calculatorForm" method="POST">
Would POST still work, even if I am using one form?
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Thu Apr 30, 2009 2:00 pm
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.
hoku_2000
Forum Newbie
Posts: 8 Joined: Wed Apr 29, 2009 11:50 pm
Post
by hoku_2000 » Thu Apr 30, 2009 2:31 pm
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.
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Thu Apr 30, 2009 2:47 pm
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.
hoku_2000
Forum Newbie
Posts: 8 Joined: Wed Apr 29, 2009 11:50 pm
Post
by hoku_2000 » Thu Apr 30, 2009 3:53 pm
I got it working now. Thanks!