...

View Full Version : Can someone write a code for me???



myedinu
02-27-2007, 03:21 PM
I need a calculator to calculate numbres that I pass to variables,
So far I wrote this script that contains, also html code:
if anyone can help me, tell me where am I doing wrong:

<?

$a=$_POST['numer1'];
$b=$_POST['numer2'];
$c=$_POST['operator'];
$d=$_POST['equall'];

$rez="";

if(isset($d)) {
switch($c) {
case "+":
$rez = ($a + $b);
break;
case "-":
$rez = ($a - $b);
break;
case "*":
$rez = ($a * $b);
break;
case "/":
$rez = ($a / $b);
break;
}
}









?>

<!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>Untitled Document</title>
</head>

<body>
<table border="1" width="300" height="300">
<form method="post" action="">
<tr>
<td colspan="2">
<table border="1">
<tr>
<td width="75"><? echo $a ?></td>
<td width="75"><? echo $b ?></td>
<td width="75"><? echo $c ?></td>
<td width="75"><? echo $rez ?></td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top">
<table border="1">
<tr>
<td width="45" height="30"><input type="submit" name="numer1[]" value="9"/></td>
<td width="45" height="30"><input type="submit" name="numer1[]" value="8"/></td>
<td width="45" height="30"><input type="submit" name="numer1[]" value="7"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="9"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="8"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="7"/></td>
</tr>
<tr>
<td width="45" height="30"><input type="submit" name="numer1[]" value="6"/></td>
<td width="45" height="30"><input type="submit" name="numer1[]" value="5"/></td>
<td width="45" height="30"><input type="submit" name="numer1[]" value="4"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="6"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="5"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="4"/></td>
</tr>
<tr>
<td width="45" height="30"><input type="submit" name="numer1[]" value="3"/></td>
<td width="45" height="30"><input type="submit" name="numer1[]" value="2"/></td>
<td width="45" height="30"><input type="submit" name="numer1[]" value="1"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="3"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="2"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="1"/></td>
</tr>
<tr>
<td width="45" height="30"><input type="submit" name="numer1[]" value="0"/></td>
<td width="45" height="30"><input type="submit" name="numer1[]" value="0"/></td>
<td width="45" height="30"><input type="submit" name="numer1[]" value="0"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="0"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="0"/></td>
<td width="45" height="30"><input type="submit" name="numer2[]" value="0"/></td>
</tr>
</table>
</td>
<td valign="top">
<table border="1">
<tr>
<td width="74"><input type="submit" name="operator[]" value="+"/></td>
</tr>
<tr>
<td width="74"><input type="submit" name="operator[]" value="-"/></td>
</tr>
<tr>
<td width="74"><input type="submit" name="operator[]" value="*"/></td>
</tr>
<tr>
<td width="74"><input type="submit" name="operator[]" value="/"/></td>
</tr>
<tr>
<td width="74"><input type="submit" name="equall[]" value="="/></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
</body>
</html>

Fumigator
02-27-2007, 04:08 PM
*sniff sniff*

Smells like homework....

I can help you with a specific issue but if you just want work done then I'll send you a rate sheet.

sir pannels
02-27-2007, 04:08 PM
Works for me. Do you get an error?

I'd imagen its cause the 'operator' is tripping the code up. You will need to encase it in quotes...

change the collection of vars from what you have to this below...


$a="".$_POST['numer1']."";
$b="".$_POST['numer2']."";
$c="".$_POST['operator']."";
$d="".$_POST['equall']."";


Best of luck

myedinu
02-27-2007, 05:22 PM
It still doesn't work(calculator),
thou I don't get any errors...

sir pannels
02-28-2007, 08:33 AM
Looking at it... the PHP is fine. The problem lies in the HTML form code.

You have input tags that all have [] in the name, that is no good. In addition your form has no action and you have many fields with indentical names. Looks like you either need to tidy that up and put in a form action, or put onClick = form.submit buisness on the buttons.

This should be addressed in another forum I believe.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum