Help - Search - Members - Calendar
Full Version: Simple PHP Calculator
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
BigStacks077
I hope I can post this and it's in the right section..

A simple calculator using variables bigwink.gif .







CODE
<?php
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$var3 = $_POST['var3'];
?>
<html>
<head>
<style type="text/css">

</style>
<title>VisualCoders.Com - Calculator</title>
</head>
<body>
<div id="main">

<h2>VC Calculator</h2>
<?php
if (!isset($_POST['submit'])) {
    ?>
<form method="post" action="<?php echo $PHP_SELF;?>">
<input type="text" name="var1" maxlength="18" class="textbox" />
<input type="text" name="var2" maxlength="1" value="X" class="textbox2" />
<input type="text" name="var3" maxlength="18" class="textbox" /><br /><br />
<input type="submit" value="submit" name="submit">
</form>
<?php
} else {
?>
<?php
echo "Your result is:<br /><br />";  

if ($var2=="+")
{
$sum=$var1 + $var3;
echo "<form>
<input type='text' name='var1' maxlength='18' class='textbox'  value='".$var1."' />
<input type='text' name='var2' maxlength='1' value='".$var2."' class='textbox2' />
<input type='text' name='var3' maxlength='18' class='textbox' value='".$var3."' /><br /><br />
<input type='text' name='var2' maxlength='1' value='=' class='textbox2' /><br /><br />
<input type='text' name='var3' maxlength='18' class='textbox' value='".$sum."' /><br />
</form><br /><br /><a href='index.php'>Calculate Again</a>";
}
elseif ($var2=="-")
{
$sum=$var1 - $var3;
echo "
<form>
<input type='text' name='var1' maxlength='18' class='textbox'  value='".$var1."' />
<input type='text' name='var2' maxlength='1' value='".$var2."' class='textbox2' />
<input type='text' name='var3' maxlength='18' class='textbox' value='".$var3."' /><br /><br />
<input type='text' name='var2' maxlength='1' value='=' class='textbox2' /><br /><br />
<input type='text' name='var3' maxlength='18' class='textbox' value='".$sum."' /><br />
</form><br /><br /><a href='index.php'>Calculate Again</a>";
}
elseif ($var2=="/")
{
$sum=$var1 / $var3;
echo "
<form>
<input type='text' name='var1' maxlength='18' class='textbox'  value='".$var1."' />
<input type='text' name='var2' maxlength='1' value='".$var2."' class='textbox2' />
<input type='text' name='var3' maxlength='18' class='textbox' value='".$var3."' /><br /><br />
<input type='text' name='var2' maxlength='1' value='=' class='textbox2' /><br /><br />
<input type='text' name='var3' maxlength='18' class='textbox' value='".$sum."' /><br />
</form><br /><br /><a href='index.php'>Calculate Again</a>";
}
elseif ($var2=="*")
{
$sum=$var1 * $var3;
echo "
<form>
<input type='text' name='var1' maxlength='18' class='textbox'  value='".$var1."' />
<input type='text' name='var2' maxlength='1' value='".$var2."' class='textbox2' />
<input type='text' name='var3' maxlength='18' class='textbox' value='".$var3."' /><br /><br />
<input type='text' name='var2' maxlength='1' value='=' class='textbox2' /><br /><br />
<input type='text' name='var3' maxlength='18' class='textbox' value='".$sum."' /><br />
</form><br /><br /><a href='index.php'>Calculate Again</a>";
}
else
{
  echo "Invalid operation. Valid operations are (+,-,/,*).<br /><a href='index.php'>Calculate Again</a>";
}
echo "<br /><br />";

?>
<?php
}
?>
<div id="copyright">
Made by <a href="http://www.visualcoders.com">VisualCoders</a>.
</div>
</body>
</html>
You also need to download the images for the textbox fields.
rc69
I'm assuming you mean this to be a tutorial so i've moved it to a more proper category.

Also, instead of using a bunch of if-statements and repeating your form code all of the time, i would recommend looking into a switch statement.
BigStacks077
I was going to use switch but i wanted to get this done last night lol. I couldnt spend another minute on it, it was 5am
dotSilver
It could have been a lot easier even without using switch statements. Here's another example for this calculator.

CODE
<?php
/* Get the three fields needed */
$num1 = $_POST['num1'];
$operator = $_POST['operator'];
$num2 = $_POST['num2'];

/* Show the three fields */
print '
<form method="post" action="test.php">
    <input type="input" name="num1" value="'. $num1 .'" class="textbox" maxlength="18" />
    <select name="operator">
        <option value="'. $operator .'" selected="selected">'. $operator .'</option>
        <option value="+">+</option>
        <option value="-">-</option>
        <option value="*">*</option>
        <option value="/">/</option>
    </select>
    <input type="input" name="num2" value="'. $num2 .'" class="textbox" maxlength="18" />
    ';
    
    /* If the form has been submitted, we can show the results. */
    if( $_POST['submit'] ){
        /* We just need to check the operator first, see which one it is and get the total */
        if( $operator == '+' ){
            $total = $num1 + $num2;
        }
        if( $operator == '-' ){
            $total = $num1 - $num2;
        }
        if( $operator == '*' ){
            $total = $num1 * $num2;
        }
        if( $operator == '/' ){
            $total = $num1 / $num2;
        }
        print '
        <input type="input" name="equals" value="=" disabled="disabled" />
        <input type="input" name="total" value="'. $total .'" disabled="disabled" />
        ';
    }
    
    /* Now we can show the submit button and end the form. */
    print '
    <br />
    <input type="submit" name="submit" value="Calculate" />
</form>
';
?>
NDBoost
I just noticed, you dont have any data validation/sanitization in your script.. how do you know they're submitting a number and not some sort of php/sql statement?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.