Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP]Help - Calculator coding

Junior Spellweaver
Joined
Aug 17, 2010
Messages
161
Reaction score
14
Hey!!!

Ive coded a calculator but multiple function dont work . It shows a random number , for example : 5*5 = 10056 .

multiple code :
PHP:
<form action="results.php" method="post">
<input type=number name=number1> * <input type=number name=number2><br>
<input type=submit value=submit>
</form> etc.. are includied

results.php for multiple function
PHP:
<?php echo $_POST["number1"] . " * " . $_POST["number2"]; ?> = <?php echo $_POST["number1"] * $_POST["number2"]; ?>

Whats the problem that it doesnt show the correct value ?

---------- Post added at 03:49 PM ---------- Previous post was at 01:53 PM ----------

cmon rz coders :p !
 
Last edited:
Joined
Sep 10, 2006
Messages
2,817
Reaction score
1,417
doing math functions in echo is a bad practise I'd say, so try

PHP:
$result = $_POST["number1"]*$_POST["number2"];

echo $result;


---------- Post added at 05:10 PM ---------- Previous post was at 05:03 PM ----------

Just tried

PHP:
	$num = "5";
	$num2 = "5";
	
	 echo $num." * ".$num2; 
	 echo '=';  
	 echo $num * $num2;

on my webserver and it works just fine, so I guess your server config is fucked up somehow
 
Last edited:
Junior Spellweaver
Joined
Aug 17, 2010
Messages
161
Reaction score
14
doing math functions in echo is a bad practise I'd say, so try

PHP:
$result = $_POST["number1"]*$_POST["number2"];

echo $result;


---------- Post added at 05:10 PM ---------- Previous post was at 05:03 PM ----------

Just tried

PHP:
	$num = "5";
	$num2 = "5";
	
	 echo $num." * ".$num2; 
	 echo '=';  
	 echo $num * $num2;

on my webserver and it works just fine, so I guess your server config is fucked up somehow

with $num $num2 worked to me and before . ill try the first one !
 
duck you, I'm a dragon
Loyal Member
Joined
Apr 29, 2005
Messages
6,407
Reaction score
130
Also, aren't $_POST values always returned as strings? I know php discriminates little when it comes to variable types, but still it bothers me.
 
Infraction Baɴɴed
Loyal Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
i thought making a calculator in php was a good idea once but constantly click submit and clicking back to do another prob can get very annoying very quick.

look into calculators made in javascript.
Code:
<html>
<head>
	<title>Calculator</title>
</head>
<body>
<center>
<form name="Cal" method="post" >
	<table border="2" cellspacing="0" cellpadding="2">
		<tr bgcolor="000000">
			<td colspan="2" align="center">
				<input type="text" name="Numbers" size="16" readonly="readonly" />
			</td>
		</tr>
		<tr>
			<td>
				<table width="80" border="0">
					<tr>
						<td><input type="button" onclick="document.Cal.Numbers.value += '7'" value="  7  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value += '8'" value="  8  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value += '9'" value="  9  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value += ' / '" value="  /  " /></td>
					</tr>
					<tr>
						<td><input type="button" onclick="document.Cal.Numbers.value += '4'" value="  4  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value += '5'" value="  5  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value += '6'" value="  6  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value += ' * '" value="  x  " /></td>
					</tr>
					<tr>
						<td><input type="button" onclick="document.Cal.Numbers.value += '1'" value="  1  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value += '2'" value="  2  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value += '3'" value="  3  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value += ' - '" value="  -  " /></td>
					</tr>
					<tr>
						<td><input type="button" onclick="document.Cal.Numbers.value = ''" value="  C  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value += '0'" value="  0  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value = eval(Cal.Numbers.value)" value="  =  " /></td>
						<td><input type="button" onclick="document.Cal.Numbers.value += ' + '" value="  +  " /></td>
					<tr>
				</table>
			</td>
		</tr>
	</table>
</form>
</center>
</body>
</html>
thats mine i made but hey someone can improve on it.
 
Newbie Spellweaver
Joined
Aug 31, 2009
Messages
46
Reaction score
4
Hey!!!

Ive coded a calculator but multiple function dont work . It shows a random number , for example : 5*5 = 10056 .

multiple code :
PHP:
<form action="results.php">
<input type=number name=number1> * <input type=number name=number2><br>
<input type=submit value=submit>
</form> etc.. are includied

results.php for multiple function
PHP:
<?php echo $_POST["number1"] . " * " . $_POST["number2"]; ?> = <?php echo $_POST["number1"] * $_POST["number2"]; ?>

Whats the problem that it doesnt show the correct value ?

---------- Post added at 03:49 PM ---------- Previous post was at 01:53 PM ----------

cmon rz coders :p !

default method is GET so : method="post"
 
Junior Spellweaver
Joined
Aug 17, 2010
Messages
161
Reaction score
14
default method is GET so : method="post"

i made code myself on this thread , i forgot to add somethings like method etc.. sorry . ill edit the code now .
in the calculator page theyre all includied ! so problem still conteniuing !

$holthelper
Well , i dont code javascript so i made it PHP !

---------- Post added at 04:40 AM ---------- Previous post was at 04:29 AM ----------

more things ive tried :
index.php :
PHP:
<?php
$form="<form action=results.php method=post>";
$x = "<input type=text name=x>";
$y = "<input type=text name=y>";
$submit = "<input type=submit>";
echo $form . " " . $x . " * " . $y . " <br> " . $submit;
?>

results.php
PHP:
<?php
$result = $_POST["x"] * $_POST["y"];  //thanks to foxx for this!

echo $result;

still doesnt work ..
5*5 = 0! :?:
 
Last edited:
Junior Spellweaver
Joined
Aug 17, 2010
Messages
161
Reaction score
14
o thanks ! ill try this too!

---------- Post added at 06:24 AM ---------- Previous post was at 05:46 AM ----------

Thanks foxx. works perfectly.
 
Junior Spellweaver
Joined
Aug 17, 2010
Messages
161
Reaction score
14
i said it , and i say it again : I MADE THE CODE MYSELF ON THE THREAD , IN MY WEB DIRECTORY ALL ARE INCLUDIED !

---------- Post added at 11:12 AM ---------- Previous post was at 10:44 AM ----------

Everyone enjoy :
PHP:
<h1>Simple calculator - Usable</h1>
<h3>Source by foxx - thank you and edited by StronGCoder</h3>
<?php
// source by foxx - thanks!
// edited by strong
// advanced calculator coming soon!
$result = "";
if (isset($_POST['val'])) {
	$val = (float)$_POST['val'];
	$val2 = (float)$_POST['val2'];

	if (!$val and $val2) {
		$result .= "<p>Fill all fields , please!</p>";
	}

	
	if (!$result) {
		switch ($_POST['operator']) {
			case "sum":
				$result = $val.'+'.$val2.' = '.$val+$val2;
				break;
			case "sub":
				$result = $val.'-'.$val2.' = '.$val-$val2;
				break;	
			case "mul":
				$result = $val.'*'.$val2.' = '.$val*$val2;
				break;	
			case "div":
				$result = $val.'/'.$val2.' = '.$val/$val2;
				break;	
		}
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="cz" lang="cz"> 
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta http-equiv="Content-Language" content="en" />

	<title>Calculator by StronGCoder</title>
</head>

<body>
	<form method="post" action="/paying/index.php">
		<dl>
			<label for="val"></label>
			<input type="text" name="val" id="val" value='First number' />
			
			<label for="operator"></label>
			
				<select name="operator" id="operator">
					<option value="Choose">Choose one :</option>					
					<option value="sum">+</option>
					<option value="sub">-</option>
					<option value="mul">*</option>
					<option value="div">/</option>
				</select>
			</dd>
			
			<label for="val2"></label>
			<input type="text" name="val2" id="val2" value='Second number' />
		
			<input type="submit" name="submit" value='Calculate' />

		</dl>
	</form>

	<?php
		echo $result;
	?>
</body>

</html>
 
Joined
Sep 10, 2006
Messages
2,817
Reaction score
1,417
Lol what is that h1 and h3 doing before html tag and where did all those dts and dd disappear to.

Also
PHP:
    if (!$val and $val2) 
doesn't make sense it will throw error only if first value was not filled and second was.

You kinda edited my code so it doesn't work anymore.
 
Junior Spellweaver
Joined
Apr 12, 2009
Messages
104
Reaction score
10
PHP:
<?php
define("StronGCoder","Confused");
?>

On a serious note, you do not have to attack everyone offering suggestions to improve your coding. If you really take coding seriously, learning to do things correctly and taking criticism is exceedingly important. If you go on the defensive every time offers a suggestion, you are not going to get anywhere fast. Admit you're wrong and continue on correctly. Acting like you're right despite the fact that you ARE wrong makes you look terrible. Realizing you're wrong and asking for help looks intelligent. The opposite looks childish.

By the way, if you weren't wrong, why did you come here to ask for help in the first place?
 
Last edited:
Back
Top