I'm creating a basic PHP calculator that lets you enter two values and chose your operator then displays the answer. Everything is working fine except it's not outputting the answer to the browser.
Here are the codes for my html and PHP files:
<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<form method="post" attribute="post" action="disp_form.php">
<p>First Value:<br/>
<input type="text" id="first" name="first"></p>
<p>Second Value:<br/>
<input type="text" id="second" name="second"></p>
<input type="radio" name="group1" id="add" value="add" checked="true"><p>+</p><br/>
<input type="radio" name="group1" id="subtract" value="subtract"><p>-</p><br/>
<input type="radio" name="group1" id="times" value="times"><p>x</p><br/>
<input type="radio" name="group1" id="divide" value="divide"><p>/</p><br/>
<p></p>
<button type="submit" name="answer" id="answer" value="answer">Calculate</button>
</form>
</body>
</html>
PHP file:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
if($_POST['group1'] == add) {
echo "$first + $second";
}
else if($_POST['group1'] == subtract) {
echo "$first - $second";
}
else if($_POST['group1'] == times) {
echo "$first * $second";
}
else($_POST['group1'] == divide) {
echo "$first / $second";
}
?>
</p>
</body>
</html>
<?php
$result = "";
class calculator
{
var $a;
var $b;
function checkopration($oprator)
{
switch($oprator)
{
case '+':
return $this->a + $this->b;
break;
case '-':
return $this->a - $this->b;
break;
case '*':
return $this->a * $this->b;
break;
case '/':
return $this->a / $this->b;
break;
default:
return "Sorry No command found";
}
}
function getresult($a, $b, $c)
{
$this->a = $a;
$this->b = $b;
return $this->checkopration($c);
}
}
$cal = new calculator();
if(isset($_POST['submit']))
{
$result = $cal->getresult($_POST['n1'],$_POST['n2'],$_POST['op']);
}
?>
<form method="post">
<table align="center">
<tr>
<td><strong><?php echo $result; ?><strong></td>
</tr>
<tr>
<td>Enter 1st Number</td>
<td><input type="text" name="n1"></td>
</tr>
<tr>
<td>Enter 2nd Number</td>
<td><input type="text" name="n2"></td>
</tr>
<tr>
<td>Select Oprator</td>
<td><select name="op">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value=" = "></td>
</tr>
</table>
</form>
Personally I would do a switch instead of all this if, else if, else
$first = $_POST['first'] + 0;//a small "hack" to make sure its an int but allow negs!!
$second= $_POST['second'] + 0;
$operator = $_POST["group1"];
switch($operator)
{
case "add"
echo "Answer is: " .$first + $second;
break;
case "subtract"
echo "Answer is: " .$first - $second;
break;
case "times"
echo "Answer is: " .$first * $second;
break;
case "divide"
echo "Answer is: " .$first / $second;
break;
}
You need to assign $first and $second
$first = $_POST['first'];
$second= $_POST['second'];
Also, As Travesty3 said, you need to do your arithmetic outside of the quotes:
echo $first + $second;
You also need to put the [== 'add'] math operation into quotes
if($_POST['group1'] == 'add') {
echo $first + $second;
}
complete code schould look like that :
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == 'add') {
echo $first + $second;
}
else if($_POST['group1'] == 'subtract') {
echo $first - $second;
}
else if($_POST['group1'] == 'times') {
echo $first * $second;
}
else if($_POST['group1'] == 'divide') {
echo $first / $second;
}
?>
You need to get the values the same way to get the calculator operation which looks like:
<?php
if($_POST['group1'] == add) {
echo "$_POST['first']+ $_POST['second'];
}
... and so on
?>
Or, to make it easier, just do:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == add) {
echo "$first + $second";
}
else if($_POST['group1'] == subtract) {
echo "$first - $second";
}
else if($_POST['group1'] == times) {
echo "$first * $second";
}
else($_POST['group1'] == divide) {
echo "$first / $second";
}
?>
</p>
</body>
</html>
$first = doubleval($_POST['first']);
$second = doubleval($_POST['second']);
if($_POST['group1'] == 'add') {
echo "$first + $second = ".($first + $second);
}
// etc
Check string using single quotes
Ex. $_POST['group1'] == 'add'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Calculator</title>
</head>
<body>
HTML Code is here:
<form method="post">
<input type="text" name="numb1">
<input type="text" name="numb2">
<select name="operator" id="">
<option>None</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
<option>Square</option>
</select>
<button type="submit" name="submit" value="submit">Calculate</button>
</form>
PHP Code:
<?php
if (isset($_POST['submit'])) {
$result1 = $_POST['numb1'];
$result2 = $_POST['numb2'];
$operator = $_POST['operator'];
switch ($operator) {
case 'None':
echo "You need to select any operator";
break;
case 'Add':
echo $result1 + $result2;
break;
case 'Subtract':
echo $result1 - $result2;
break;
case 'Multiply':
echo $result1 * $result2;
break;
case 'Divide':
echo $result1 / $result2;
break;
case 'Square':
echo $result1 ** $result2;
break;
}
}
?>
enter code here
</body>
</html>
Make this changes in php file
<html>
<head>
<meta charset="utf-8">
<title>Answer</title>
</head>
<body>
<p>The answer is:
<?php
$first = $_POST['first'];
$second= $_POST['second'];
if($_POST['group1'] == 'add') {
echo $first + $second;
}
else if($_POST['group1'] == 'subtract') {
echo $first - $second;
}
else if($_POST['group1'] == 'times') {
echo $first * $second;
}
else if($_POST['group1'] == 'divide') {
echo $first / $second;
}
else {
echo "Enter the numbers properly";
}
?>
</p>
</body>
</html>
<?php
$cal1= $_GET['cal1'];
$cal2= $_GET['cal2'];
$symbol =$_GET['symbol'];
if($symbol == '+')
{
$add = $cal1 + $cal2;
echo "Addition is:".$add;
}
else if($symbol == '-')
{
$subs = $cal1 - $cal2;
echo "Substraction is:".$subs;
}
else if($symbol == '*')
{
$mul = $cal1 * $cal2;
echo "Multiply is:".$mul;
}
else if($symbol == '/')
{
$div = $cal1 / $cal2;
echo "Division is:".$div;
}
else
{
echo "Oops ,something wrong in your code son";
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Calculator</title>
</head>
<body>
<form method="GET">
<h1>Calculator</h1>
<p>This is a calculator made by Hau Teen Yee Fabrice and is free to use.</p>
<ul>
<ol> Multiplication: * </ol>
<ol> Substraction: - </ol>
<ol> Division: / </ol>
<ol> Addition: + </ol>
</ul>
<p>Enter first number:</p>
<input type="text" name="cal1">
<p>Enter second number:</p>
<input type="text" name="cal2">
<p>Enter the calculator symbol</p>
<input type="text" name="symbol">
<button>Calculate</button>
</form>
<?php
$cal1= $_GET['cal1'];
$cal2= $_GET['cal2'];
$symbol =$_GET['symbol'];
if($symbol == '+')
{
$add = $cal1 + $cal2;
echo "Addition is:".$add;
}
else if($symbol == '-')
{
$subs = $cal1 - $cal2;
echo "Substraction is:".$subs;
}
else if($symbol == '*')
{
$mul = $cal1 * $cal2;
echo "Multiply is:".$mul;
}
else if($symbol == '/')
{
$div = $cal1 / $cal2;
echo "Division is:".$div;
}
else
{
echo "Oops ,something wrong in your code son";
}
?>
</body>
</html>
Related
Hi im very new to this stuff. My PHP calculator returns nothing when I hit calculate, when I upload this file to my 000webhost site. I don't have the website up right now but here is my code.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="get" action="#">
Enter the First Number <input type="text" name="number1"> <br><br>
Enter the Second Number <input type="text" name="number2"><br><br>
Choose the operation
Addition <input type="radio" name="op" value="add" checked>
Subtraction <input type="radio" name="op" value="sub">
Multiplication <input type="radio" name="op" value="mul">
Division<input type="radio" name="op" value="div"><br><br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
<?php
if ( $op == "add" ) {
echo "$number1 + $number2 = ";
echo $number1 + $number2;
}
if ( $op == "sub" ) {
echo "$number1 - $number2 = ";
echo $number1 - $number2;
}
if ( $op == "mul" ) {
echo "$number1 * $number2 = ";
echo $number1 * $number2;
}
if ( $op == "div" ) {
echo "$number1 / $number2 = ";
echo $number1 / $number2;
}
?>
<html>
When I hit calculate nothing happens and the link gets a question mark in it. Please help.
You need to use $_GET for accessing input value from get form request
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="get" >
Enter the First Number <input type="text" name="number1"> <br><br>
Enter the Second Number <input type="text" name="number2"><br><br>
Choose the operation
<br>
<input type="radio" name="op" value="add" checked>Addition<br>
<input type="radio" name="op" value="sub">Subtraction<br>
<input type="radio" name="op" value="mul">Multiplication<br>
<input type="radio" name="op" value="div">Division<br><br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
<?php
if (isset( $_GET['number1']) && isset( $_GET['number2']) && isset( $_GET['op'])) {
$number1 = $_GET['number1'];
$number2 = $_GET['number2'];
$op = $_GET['op'];
if ($op == "add") {
echo "$number1 + $number2 = ";
echo $number1 + $number2;
}
if ($op == "sub") {
echo "$number1 - $number2 = ";
echo $number1 - $number2;
}
if ($op == "mul") {
echo "$number1 * $number2 = ";
echo $number1 * $number2;
}
if ($op == "div") {
echo "$number1 / $number2 = ";
echo $number1 / $number2;
}
}
?>
<html>
I'am using php 7.0, apache2, mysql ver 14.14 distribution 5.6.17, and phpmyadmin 5.7
My php file for some reason will not display the value for variable $result. Whenever I try to use "echo" it wont display it. Not even another variable (sum).
My html file has following code:
<!doctype html>
<html lang="en-us">
<head>
<title>calculation form</title>
<meta charset="utf-8">
</head>
<body>
<form method="post" action="calculate.php">
<p>Value 1: <input type="text" name="val1" size="10"></p>
<p>Value 2: <input type="text" name="val2" size="10"></p>
<p>Calculation:<br>
<input type="radio" name="calc" value="add"> add<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>
<p><input type="submit" name="submit" value="Calculate"></p>
</form>
</body>
</html>
My php code has the following:
<?php
$sum = 0;
if(($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == ""))
{
header("Location: calculate_form.html");
exit;
}
if($_POST[calc] == "add")
{
$result = $_POST[val1] + $_POST[val2];
}
if($_POST[calc] == "subtract")
{
$result = $_POST[val1] - $_POST[val2];
}
if($_POST[calc] == "multiply")
{
$result = $_POST[val1] - $_POST[val2];
}
if ($_POST[calc] == "divide") {
$result = $_POST[val1] / $_POST[val2];
}
?>
<?php
echo $result;
echo $sum;
?>
you have to echo inside if condition or you have to set variable globally
<?php
$sum = 0;
if(($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == ""))
{
header("Location: calculate_form.html");
exit;
}
if($_POST['calc'] == "add")
{
$result = $_POST['val1'] + $_POST['val2'];
echo $result;
}
if($_POST['calc'] == "subtract")
{
$result = $_POST['val1'] - $_POST['val2'];
echo $result;
}
if($_POST['calc'] == "multiply")
{
$result = $_POST['val1'] - $_POST['val2'];
echo $result;
}
if ($_POST['calc'] == "divide") {
$result = $_POST['val1'] / $_POST['val2'];
echo $result;
}
?>
or else set like this
if($_POST[calc] == "add")
{
$result = $_POST[val1] + $_POST[val2];
$sum= $result;
}
also yuo have error in $_POST[val1]
it should be $_POST['val1']
Updated with example
<?php
$sum = 0;
$_POST['val1']=5;
$_POST['val2']=10;
$_POST['calc']='add';
if(($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == ""))
{
header("Location: calculate_form.html");
exit;
}
if($_POST['calc'] == "add")
{
$sum = $_POST['val1'] + $_POST['val2'];
}
if($_POST['calc'] == "subtract")
{
$sum = $_POST['val1'] - $_POST['val2'];
}
if($_POST['calc'] == "multiply")
{
$sum = $_POST['val1'] - $_POST['val2'];
}
if ($_POST['calc'] == "divide") {
$sum = $_POST['val1'] / $_POST['val2'];
}
echo $sum;
?>
I am creating a calculator on the web to challenge my ability to think an challenge my creative edge and i got the basics down it works but the issue i am having is the error displaying whem there is no error is just displays when the page loads and then displays the value when the calculator is used can someone please help?
<?php
$value1 = $_POST['value1'];
$symbol = $_POST['operator'];
$value2 = $_POST['value2'];
$nulr = "nulrl";
if($nulr === "nulrl"){
if($symbol === "+"){
$output = $value1 + $value2;
} elseif($symbol === "-"){
$output = $value1 - $value2;
} elseif($symbol === "/"){
$output = $value1 / $value2;
} elseif($symbol === "*"){
$output = $value1 * $value2;
} else{
$output = "Error could not perform operation";
}
}
echo $output;
?>
EDIT
Here is my html code
<!DOCTYPE html>
<html lang="en">
<head>
<title>The Calculator</title>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<h1>The Goal</h1>
<p>Through my studies of coding languages i have come to realize that just about every coding langauage no matter how different the syntax can handle user input and variables and produce and output. With this being said i wanna make an interactive calculator for each coding language and then advance on each one and see which language can produce the best calculator!</p>
<main>
<h2>PHP Calculator</h2>
<form method="post">
<input type="number" name="value1" value="value1" class="number-box">
<br>
<select name="operator">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<br>
<input type ="number" name="value2" value="value2" class="number-box">
<br>
<input type ="submit" name="submit" class="submit-button">
</form>
<?include("calculator.php")?>
<footer>
<h3>Comment Box</h3>
<form method="post" action="comment.php">
<input type="text" value="name" name="name">
<br>
<textarea name="comment">Comments</textarea>
<input type="submit" name="submit" class="submit-button">
</form>
<?php
echo file_get_contents("comments.txt");
?>
</footer>
</main>
</header>
EDIT:
Still have not recieved any correct answers
The simplest way:
<?php
if(isset($_POST['value1']){
//.......
else{
$output = '';
echo "Error, could not perform operation";
}
}
echo $output;
}
?>
Put your code like this:
$http_post = ($_SERVER['REQUEST_METHOD'] == 'POST') ;
if($http_post)
{
$value1 = $_POST['value1'];
$symbol = $_POST['operator'];
$value2 = $_POST['value2'];
$nulr = "nulrl";
if($nulr === "nulrl"){
if($symbol === "+"){
$output = $value1 + $value2;
} elseif($symbol === "-"){
$output = $value1 - $value2;
} elseif($symbol === "/"){
$output = $value1 / $value2;
} elseif($symbol === "*"){
$output = $value1 * $value2;
}
else
{
$output = "Error could not perform operation";
}
}
echo (!empty($output)) ? $output : '';
If any errors occur it will appear only after post.
I tried to make a quadratic equation solver in php:
index.html:
<html>
<body>
<form action="findx.php" method="post">
Find solution for ax^2 + bx + c<br>
a: <input type="text" name="a"><br>
b: <input type="text" name="b"><br>
c: <input type="text" name="c"><br>
<input type="submit" value="Find x!">
</form>
</body>
</html>
findx.php:
<?php
if(isset($_POST['a'])){ $a = $_POST['a']; }
if(isset($_POST['b'])){ $b = $_POST['b']; }
if(isset($_POST['c'])){ $c = $_POST['c']; }
$d = $b*$b - 4*$a*$c;
echo $d;
if($d < 0) {
echo "The equation has no real solutions!";
} elseif($d = 0) {
echo "x = ";
echo (-$b / 2*$a);
} else {
echo "x1 = ";
echo ((-$b + sqrt($d)) / (2*$a));
echo "<br>";
echo "x2 = ";
echo ((-$b - sqrt($d)) / (2*$a));
}
?>
the problem is that it's returning wrong answers (d is right,x1 and x2 are not) seems like sqrt() is returning zero or maybe something else.
There's a typo in this line:
elseif($d = 0)
which is assigning the value 0 to $d instead of comparing it. That means you are always evaluating sqrt(0), which is 0, in your else block.
It should be:
elseif($d == 0)
This is form for calculator
<form method='post' action='result.php' name='calc_form'>
<input type='text' name='input1' size='15'>
<select name='operation'>
<option value="plus">+</option>
<option value="minus">-</option>
<option value="multi">*</option>
<option value="invalid">**</option>
<option value="divide">/</option>
</select>
<input type='text' name='input2' size='15'>
<input type='submit' value='go'>
</form>
This is php with if elseif statement
<?php
define ('INVALID_INPUT', 'ERROR: invalid input');
define ('INVALID_OPERATOR', 'ERROR: invalid operator');
$result = null;
if
(isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
{
$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
$operation = $_POST['operation'];
switch ($operation)
{
case 'plus':
$result = $input1 + $input2;
break;
case 'minus':
$result = $input1 - $input2;
break;
case 'multi':
$result = $input1 * $input2;
break;
case 'divide':
$result = $input1 / $input2;
break;
default:
$result = INVALID_OPERATOR;
break;
}
}
elseif
(!isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
{
$result = INVALID_INPUT;
}
if ($result !== null)
{
echo <<<EOM
<h2>you calculate</h2> $input1
<h2>and</h2> $input2
<h2>result is:</h2>
$result
EOM;
}
?>
I even tried with only else statement and without condition but it wont show INVALID INPUT when nothing is added in form. What is wrong here?
The problem is with this piece of code:
if
(isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
Even if a user enters nothing in the input fields, they will still pass isset($_POST['input1']), since they are 'set' to an empty string. Try switching it to this instead:
if
(isset($_POST['input1']) && strlen($_POST['input1']) &&
isset($_POST['input2']) && strlen($_POST['input2']) &&
isset($_POST['operation']))
You could just use an else statement instead of an else if statement. That way if you change the inputs you don't have to list each of them twice.
eg:
if (isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation']))
{
// result = calculation
} else
{
// result == code
}
Try this - I tested this on my server and it works!
calculator.php
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors',1);;
define ('INVALID_INPUT', 'ERROR: invalid input');
define ('INVALID_OPERATOR', 'ERROR: invalid operator');
$input1 = $input2 = $operation = null;
if (isset($_POST['input1']) and isset($_POST['input2']) and isset($_POST['operation']))
{
$input1 = $_POST['input1'];
$input2 = $_POST['input2'];
$operation = $_POST['operation'];
if(is_numeric($input1) and is_numeric($input2))
{
switch ($operation)
{
case 'plus':
$result = $input1 + $input2;
break;
case 'minus':
$result = $input1 - $input2;
break;
case 'multi':
$result = $input1 * $input2;
break;
case 'divide':
$result = $input1 / $input2;
break;
default:
$result = INVALID_OPERATOR;
break;
}
}
else
{
$input1 = '';
$input2 = '';
$operation = 'invalid';
$result = INVALID_INPUT;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body class="">
<form method='POST' action='calculator.php'>
<input type='text' name='input1' size='15' value="<?php echo $input1; ?>">
<select name='operation'>
<option <?php if($operation == 'plus') { echo 'selected';} ?> value="plus">+</option>
<option <?php if($operation == 'minus') { echo 'selected';} ?> value="minus">-</option>
<option <?php if($operation == 'multi') { echo 'selected';} ?> value="multi">*</option>
<option <?php if($operation == 'invalid') { echo 'selected';} ?> value="invalid">**</option>
<option <?php if($operation == 'divide') { echo 'selected';} ?> value="divide">/</option>
</select>
<input type='text' name='input2' size='15' value="<?php echo $input2; ?>">
<span>=</span>
<input type='text' name='result' size='50' value="<?php echo $result; ?>">
<input type='submit' value='go'>
</body>
</html>
Your condition is wrong; this is impossible:
!isset($_POST) and
isset($_POST['input1']) and
isset($_POST['input2']) and
isset($_POST['operation'])
I suspect you mean
!isset($_POST) or
!isset($_POST['input1']) or
!isset($_POST['input2']) or
!isset($_POST['operation'])