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'])
Related
Could someone help me with learning PHP? I'm want to create simply form and after adding 'isset function' is returning all of it.
<html>
<body>
<div id="main">
<form action = "" method = 'POST'>
<select name = "something">
<option value = "Monitors">Monitors</option>
<option value = "Graphics">Graphics</option>
<option value = "Peripherials">Peripherials</option>
<option value = "Processors">Processors</option>
</select>
<input type = "submit" value = "OK">
</form>
</div>
<?php
if (isset($_POST["something"]) == "Monitors")
{
echo "Monitors <br />";
}
if (isset($_POST["something"]) == "Graphics")
{
echo "Graphics <br />";
}
if (isset($_POST["something"]) == "Peripherials")
{
echo "Peripherials <br />";
}
if (isset($_POST["something"]) == "Processors")
{
echo "Processors <br />";
}
?>
</body>
</html>
It returns all 'echo' after selecting one of option in form.
Without using 'isset' is okey but then returns error with 'undefined variable'.
Where i made a mistake?
Reason:- Based on PHP: isset - Manual
Returns TRUE if var exists and has value other than NULL. FALSE
otherwise.
Because of that your if condition becomes:-
if(true == 'Monitors'){
which always treated as true and every-thing is printing.
Solution:-
Change if conditions like below:-
if(isset($_POST["something"]) && $_POST["something"]== "Monitors"){
And so-on for others.
Also your Php code is unnecessary long, make it short like below:-
<?php
if (isset($_POST["something"])){
echo $_POST["something"]; // add ."<br />" if you needed
}
?>
So full code need to be:-
<html>
<body>
<div id="main">
<form action = "" method = 'POST'>
<select name = "something">
<option value = "Monitors">Monitors</option>
<option value = "Graphics">Graphics</option>
<option value = "Peripherials">Peripherials</option>
<option value = "Processors">Processors</option>
</select>
<input type = "submit" value = "OK">
</form>
</div>
<?php
if (isset($_POST["something"])){
echo $_POST["something"]; // add ."<br />" if you needed
}
?>
isset returns boolen values, do this instead; what I have done is check if $_POST["something"] is set and $_POST["something"] is one of select option values
<html>
<body>
<div id="main">
<form action = "" method = 'POST'>
<select name = "something">
<option value = "Monitors">Monitors</option>
<option value = "Graphics">Graphics</option>
<option value = "Peripherials">Peripherials</option>
<option value = "Processors">Processors</option>
</select>
<input type = "submit" value = "OK">
</form>
</div>
<?php
if (isset($_POST["something"]) && $_POST["something"] == "Monitors")
{
echo "Monitors <br />";
}
if (isset($_POST["something"]) && $_POST["something"] == "Graphics")
{
echo "Graphics <br />";
}
if (isset($_POST["something"]) && $_POST["something"] == "Peripherials")
{
echo "Peripherials <br />";
}
if (isset($_POST["something"]) && $_POST["something"] == "Processors")
{
echo "Processors <br />";
}
?>
</body>
</html>
<html>
<body>
<div id="main">
<form action = "" method = 'POST'>
<select name = "something">
<option value = "Monitors">Monitors</option>
<option value = "Graphics">Graphics</option>
<option value = "Peripherials">Peripherials</option>
<option value = "Processors">Processors</option>
</select>
<input type = "submit" value = "OK">
</form>
</div>
<?php
// If you want to only one value then you can use below. it will echo selected option
if (isset($_POST["something"])) {
echo $_POST["something"];
}
//you can check isset and compare post value.
if (isset($_POST["something"]) && $_POST["something"] == "Monitors")
{
echo "Monitors <br />";
}
if (isset($_POST["something"]) && $_POST["something"] == "Graphics")
{
echo "Graphics <br />";
}
if (isset($_POST["something"]) && $_POST["something"] == "Peripherials")
{
echo "Peripherials <br />";
}
if (isset($_POST["something"]) && $_POST["something"] == "Processors")
{
echo "Processors <br />";
}
?>
</body>
</html>
Try using Switch Statement
<?php
if(isset($_POST['something'])){
$var =$_POST["something"];
switch ($var) {
case "Monitors":
echo "Monitors!";
break;
case "Graphics":
echo "Graphics!";
break;
case "Peripherials":
echo "Peripherials";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
}
?>
The result of isset is, as has been stated, a boolean - which can be tested for in a logic operation( ie: if true then ) ~ the switch statement just makes a cleaner job of multiple if/then/else conditions
if( isset( $_POST['something'] ) ){
switch( $_POST['something'] ){
case 'Monitors':$item='Monitors';break;
case 'Graphics ':$item='Graphics ';break;
case 'Peripherials ':$item='Peripherials ';break;
case 'Processors ':$item='Processors ';break;
}
printf('%s<br />', $item);/* use $item somewhere/somehow*/
}
You should use the condition like if (isset($_POST["something"]) and $_POST["something"]== "Monitors")
Because isset() return true or false as return value .
Please find update code as below.
<html>
<body>
<div id="main">
<form action = "" method = 'POST'>
<select name = "something">
<option value = "Monitors">Monitors</option>
<option value = "Graphics">Graphics</option>
<option value = "Peripherials">Peripherials</option>
<option value = "Processors">Processors</option>
</select>
<input type = "submit" value = "OK">
</form>
</div>
<?php
if (isset($_POST["something"]) and $_POST["something"]== "Monitors")
{
echo "Monitors <br />";
}
if (isset($_POST["something"]) and $_POST["something"] == "Graphics")
{
echo "Graphics <br />";
}
if (isset($_POST["something"]) and $_POST["something"] == "Peripherials")
{
echo "Peripherials <br />";
}
if (isset($_POST["something"]) and $_POST["something"] == "Processors")
{
echo "Processors <br />";
}
?>
</body>
</html>
You can use this:
if (isset($_POST["something"]) && $_POST["something"] == "Monitors")
{
echo "Monitors <br />";
}
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'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>
Hello i'm trying to make a calculator.And i have this problem : I'm tryng to make the selection that i made it to stay after submit. I've found some code on google for my selection to stay after submit but my calculator won't work anymore. Can you help me ?
here is the code when my calculator works but my selection doesn"t stays after submit
<html>
<body>
<center>
<form method="post">
Food:
<Select name="Dropdown"><br>
<option>Cheese</option>
<option>Apple</option>
<option>Egg</option>
</select>
</br>
Amount:
<input name="amount" type="text">grams<br><br>
<br><input type="Submit" value="Calculate">
<br><br>
<?php
$result=$_POST['result'];
$Dropdown=$_POST['Dropdown'];
$amount = $_POST['amount'];
switch ($Dropdown){
case 'Cheese':
$result= (7.74 * $amount) / 100;
echo "<p> $result </p>";
break;
case 'Apple':
$result= (1.94 * $amount) / 100;
echo "<p> $result </p>";
break;
case 'Egg':
$result= (13.74 * $amount) / 100;
echo "<p> $result </p>";
}
?>
</form>
</center>
</body>
</html>
and here is my code when my slection stays after submit but my calculator won't work
<html>
<head></head>
<body>
<center>
<?php
if (!empty($_POST['Dropdown'])) {
$dropDownVal = $_POST['Dropdown'];
} else {
$dropDownVal = 1;
}
?>
<form method="post">
<select name="Dropdown" >
<option value="1" <?php if ($dropDownVal==1) echo 'selected="selected"'; ?>>Cheese </option>
<option value="2" <?php if ($dropDownVal==2) echo 'selected="selected"'; ?>>Apple</option>
<option value="3" <?php if ($dropDownVal==3) echo 'selected="selected"'; ?>>Egg</option>
</select>
<?php
$result=$_POST['result'];
$Dropdown=$_POST['Dropdown'];
$amount = $_POST['amount'];
switch ($Dropdown){
case 'Cheese':
$result= (7.74 * $amount) / 100;
echo "<p> $result </p>";
break;
case 'Apple':
$result= (1.94 * $amount) / 100;
echo "<p> $result </p>";
break;
case 'Egg':
$result= (13.74 * $amount) / 100;
echo "<p> $result </p>";
}
?>
<input name="amount" type="text">grams<br><br>
<br><input type="Submit" value="Calculate">
</form>
</center>
</body>
</html>
Thanks
What you receive back in the $_POST['Dropdown'] is the value not the content so you will get 1 or 2 or 3 and not the Cheese or Apple or Egg.
So try:
switch ($Dropdown){
case 1: // Cheese
$result= (7.74 * $amount) / 100;
break;
case 2: //Apple
$result= (1.94 * $amount) / 100;
break;
case 3: // Egg
$result= (13.74 * $amount) / 100;
break;
default:
$result = 0;
}
echo "<p> $result </p>";
?>
In future if you are not sure what is in a variable that is returned from the user do a
echo '<pre>' . print_r( $_POST, TRUE ) . '</pre>';
to get a nice display of the array on the browser.
Or a
var_dump( $var );