Calculator

SpaceshipSpaceship
Watch

DJL2K

Pimp Master MEstablished Member
Impact
0
EDIT: Fixed. Nevermind people
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
lol just as I fixed it

PHP:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Calculator</title>
</head>

<body>
<form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
  <input type="text" name="numa" size="10">
  <select name="operator">
    <option>x</option>
    <option>÷</option>
    <option>+</option>
    <option>-</option>
  </select>
  <input type="text" name="numb" size="10">
  <input type="submit" value="Calculate" name="submit">
</form>
<?php

if(!empty($_POST['submit'])) {
	$numa = $_POST['numa'];
	$numb = $_POST['numb'];
	$operator = $_POST['operator'];
	$submit = $_POST['submit'];
    if($operator == 'x') {
        echo $numa * $numb;
        }
    elseif($operator == '÷') {
        echo $numa / $numb;
        }
    elseif($operator == '+') {
        echo $numa + $numb;
        }
    elseif($operator == '-') {
        echo $numa - $numb;
        }
    }
?>
</body>
</html>

You had
PHP:
while ($submit) {
which will always be true and cause an infinite loop (hence why you had the max execution timeout, you also most likely had a huge screen with the equation repeated over and over).

You also had quotes around the echo lines that do the calculations and that was causing the mathematical symbol to be seen as a string instead of operator (hence why it just printed the equation instead of working out the answer)

lastly you did not check the existance of the post vars which causes an undefined index error if the form was not submitted. I havnt exactly fixed it but it will have helped.

If you wish to improve the script other things you can do is validate all user input to ensre it is as expected. For example numa and numb do not currently HAVE to be numbers although during the equation they should automatically be made into numbers (ie the letters stripped).
 
Last edited:
0
•••
Dynadot — .com TransferDynadot — .com Transfer
CatchedCatched

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back