Help with calculator

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

Post Reply
builder77
Forum Newbie
Posts: 2
Joined: Wed Nov 10, 2004 5:11 pm

Help with calculator

Post by builder77 »

A simple calculator when i click a number it will not show up in the input box.

Any ideas?

Thanks
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

code?
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

is this in PHP? doesnt sound like something you would wanna do with php 8O
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i would do it something like this, needs some input validation etc... but the main idea is clear (i hope):

Code: Select all

<?php

function evaluate($str)
{
    $value = '$p=' . $str . ";";
    eval($value);
    return $p;
}

session_start();
$expression = $_SESSION['foo'];

if (isset($_POST['equals']))
{
     $value = evaluate("$expression");
     $expression = '';
}
else if ($_POST['foo'])
{
     $value = $_POST['foo'];
     $expression .= $_POST['foo'];
}

$_SESSION['foo'] = $expression;
?>

Code: Select all

&lt;form method="post"&gt;
&lt;input type="text" value="&lt;?php echo $value; ?&gt;" /&gt;
&lt;input type="submit" name="equals" value="=" /&gt;
&lt;input type="submit" name="foo" value="+" /&gt;
&lt;input type="submit" name="foo" value="1" /&gt;
&lt;/form&gt;
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

heres a very simple one written in javascript

Code: Select all

<form>
<p><input type="text" name="input" id="input" /></p>

<p><input type="button" name="one"   value="  1  " onclick="form.input.value += '1'" />
<input type="button" name="two"   value="  2  " onclick="form.input.value += '2'" />
<input type="button" name="three" value="  3  " onclick="form.input.value += '3'" />
<input type="button" name="plus"  value="  +  " onclick="form.input.value += ' + '" /></p>

<p><input type="button" name="four"  value="  4  " onclick="form.input.value += '4'" />
<input type="button" name="five"  value="  5  " onclick="form.input.value += '5'" />
<input type="button" name="six"   value="  6  " onclick="form.input.value += '6'" />

<input type="button" name="minus" value="  -  " onclick="form.input.value += ' - '" /></p>

<p><input type="button" name="seven" value="  7  " onclick="form.input.value += '7'" />
<input type="button" name="eight" value="  8  " onclick="form.input.value += '8'" />
<input type="button" name="nine"  value="  9  " onclick="form.input.value += '9'" />
<input type="button" name="times" value="  x  " onclick="form.input.value += ' * '" /></p>

<p><input type="button" name="clear" value="  C  " onclick="form.input.value = ''" />
<input type="button" name="zero"  value="  0  " onclick="form.input.value += '0'" />
<input type="button" name="doit"  value="  =  " onclick="form.input.value = eval(form.input.value)" />
<input type="button" name="div"   value="  /  " onclick="form.input.value += ' / '" /></p>

</form>
builder77
Forum Newbie
Posts: 2
Joined: Wed Nov 10, 2004 5:11 pm

This is my code

Post by builder77 »

<form action="formcalc2.php" method="get">
<input type= "text" name="number1" size="15">
<br>
<input type= "text" name="number2" size="15">

//Then I code all my submit buttons (0-9, +, -, *, /, and =)

//Click the buttons and input data into the text field

if ($_GET['one'] == true) {
print '1'; }

if ($_GET['two'] == true) {
print '2'; }

//And all the buttons are coded the same, etc. Instead of printing 1 and 2 on a separate line, I need to print them in my text input boxes and be able to concatenate to them.

Any suggestions?
Post Reply