Page 1 of 1

Help with calculator

Posted: Wed Nov 10, 2004 5:13 pm
by builder77
A simple calculator when i click a number it will not show up in the input box.

Any ideas?

Thanks

Posted: Wed Nov 10, 2004 5:18 pm
by rehfeld
code?

Posted: Wed Nov 10, 2004 5:23 pm
by Deemo
is this in PHP? doesnt sound like something you would wanna do with php 8O

Posted: Wed Nov 10, 2004 5:46 pm
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;

Posted: Wed Nov 10, 2004 5:49 pm
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>

This is my code

Posted: Wed Nov 10, 2004 7:51 pm
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?