Hallo, I'm very new to php so I started off with creating a simple calculator now every time I'm testing it an error occurs that something is wrong on line 2 when I try to calculate something can somebody help me out? HTML CODE: <!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="calculate.php"> <p>Value 1.<input type="text" name="value1" width="15" height="8" /></p> <p>Value 2.<input type="text" name="value2" height="8" width="15" /></p> <p>Calculation:<br> <input type="radio" name="calc" value="addition"/>Addition<br> <input type="radio" name="calc" value="substract"/> Substract<br> <input type="radio" name="calc" value="multiply"/>Multiply<br> <input type="radio" name="calc" value="divide"/>Divide</p> <input type="submit" name="submit" value="calculate"/> </form> </body> </html> HTML: PHP CODE: <?php if ((if $_POST [value1] =="" ) || (if $_POST[value2] =="") || ($_POST[calc] =="")) { header(Location: "index.html"); exit; ) if ( $_POST[calc] =="addidtion") { $result ==($_POST[value1]) + ($_POST[value2]}; else if ($_POST[calc] =="substract") { $result == $_POST[value1] - $_POST[value2]}; else if ($_POST[calc] =="multiply"} { $result == $_POST[value1] * $_POST[value2]}; else if($_POST[calc] =="divide") { $result == $_POST[value1] / $_POST[valu2]}; echo"<title>Calculation:</title>"; echo"<br>The result of the calculation is: $result"; ?> Code (markup):
it happens sometimes... this was what you did wrong if ((if $_POST [value1] =="" ) || (if $_POST[value2] =="") || ($_POST[calc] =="")) { PHP: and this is how it looks after correction: if (($_POST [value1] =="" ) || ($_POST[value2] =="") || ($_POST[calc] =="")) { PHP: there were unnecessary if's cheers...
Hahaha oke, thanks but you say they are unnecessary that means it shouldn't give an error when I still use them?
{ $result ==($_POST[value1]) + ($_POST[value2]}; You really want to be santizing this and you're missing a closing ) it seems plus consistent spacing will make it easier to look at and go through
This should get you started: <form method="post"> <p>Value 1.<input type="text" name="value1" width="15" height="8" /></p> <p>Value 2.<input type="text" name="value2" height="8" width="15" /></p> <p>Calculation:<br> <input type="radio" name="calc" value="addition"/>Addition<br> <input type="radio" name="calc" value="subtract"/>Subtract<br> <input type="radio" name="calc" value="multiply"/>Multiply<br> <input type="radio" name="calc" value="divide"/>Divide</p> <input type="submit" name="submit" value="calculate" /> </form> <?php if (isset($_POST['submit']) && isset($_POST['calc'])) { if (!is_numeric($_POST['value1']) || !is_numeric($_POST['value2'])) { echo "Please make sure the value's are filled in and are numbers."; } else { $calculation = $_POST['calc']; $value_one = intval($_POST['value1']); $value_two = intval($_POST['value2']); if ($calculation == "addition") { $result = $value_one + $value_two; } elseif ($calculation == "subtract") { $result = $value_one - $value_two; } elseif ($calculation == "multiply") { $result = $value_one * $value_two; } elseif ($calculation == "divide") { $result = $value_one / $value_two; } echo "The result of the calculation is: {$result}"; } } ?> PHP: Also look into: http://www.tizag.com/phpT/operators.php