Jump to content


Photo
- - - - -

Simple PHP Calculator


  • Please log in to reply
4 replies to this topic

#1 BigStacks077

BigStacks077

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 02 August 2009 - 03:53 AM

I hope I can post this and it's in the right section..

A simple calculator using variables :o .

Posted Image

Posted Image

Posted Image

<?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.

Attached Images

  • text.png
  • text2.png

Edited by BigStacks077, 02 August 2009 - 03:53 AM.

  • Fafequave and AaMuckedGr like this

#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 02 August 2009 - 11:09 AM

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.

#3 BigStacks077

BigStacks077

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 02 August 2009 - 11:56 AM

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

#4 dotSilver

dotSilver

    Young Padawan

  • Members
  • Pip
  • 32 posts

Posted 12 September 2009 - 05:06 PM

It could have been a lot easier even without using switch statements. Here's another example for this calculator.

<?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>
';
?>


#5 NDBoost

NDBoost

    Young Padawan

  • Members
  • Pip
  • 27 posts
  • Gender:Male
  • Location:Tempe, AZ

Posted 17 September 2009 - 12:55 PM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users