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!
Hi guys, I'm a newbie and just gettin the hang of php. I am making a simple calculator which works when I manually give the operator like so...
$a + $b = $c
But I'm trying to make the operator itself a variable from a form and can't figure it out...I'm sure this is elementary for you guys...but could you help me out here. This is what I go t so far...
<h1>The Calculator</h1>
<form method="post" action="dbselecct.php">
<input type="text" name="box1" />
<input type="text" name="kind" />
<input type="text" name="box2" />
<br/>
<input type="submit" value="Calculate" /><br/>
</form>
Total =
<?php
$box1 = $_POST['box1'];
$kind = $_POST['kind'];
$box2 = $_POST['box2'];
$total = ??????????????? //What goes here?
//I just want to be able to put a + - / * % in the middle box,
// hit submit and have it out put the answer
echo $total;
?>
internet-solution gave you the answer, read his reply again. Use the 'switch' syntax to perform the arithmetic, based on what is in the middle (operator) box (or take his good suggestion that you use a drop down box instead of a text box for the operator, so the user is limited to valid operators which your script will recognize). Obviously this is a learning exercise for you, that's good, so this way you will learn about switch syntax and maybe drop down boxes, too.
You're welcome, that's why we spend time here, to help people who want to learn and are willing to invest their own energy into it. Hey, none of us were born knowing this stuff, we all learned it by studying and doing it. Congratulations on getting it working, good luck in your learning, and come back when you have more questions.