A simple calculator when i click a number it will not show up in the input box.
Any ideas?
Thanks
Help with calculator
Moderator: General Moderators
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
<form method="post">
<input type="text" value="<?php echo $value; ?>" />
<input type="submit" name="equals" value="=" />
<input type="submit" name="foo" value="+" />
<input type="submit" name="foo" value="1" />
</form>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
<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?
<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?