Calculator selection stays after submit - php

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 );

Related

Php calculator quantity multiplied to specified option

I'm trying to make a simple php calculator which multiplies quantity to predefined prices.
Here is my code but I got error 500. I can't manage to fix it by myself.
It's html form with $_post checks and $_post submit button
// Check if form was submitted
$product = $_POST['product'];
$quantity = $_POST['quantity'];
$first = 0,6;
$second = 0,5;
$third = 2,8;
if ( isset($_POST['submit']) ) {
switch($_POST['product']) {
case '1':
$calculation = $quantity * $first;
echo "Цена" . $calculation;
break;
case '2':
$calculation = $quantity * $second;
echo "Цена" . $calculation;
break;
case '3':
$calculation = $quantity * $third;
echo "Цена" . $calculation;
break;
default:
}
}
?>
<form action="" method="post">
<select name="product" id="product">
<option>Choose servicve</option>
<option value="1">Service 1</option>
<option value="2">Service 2</option>
<option value="3">Service 3</option>
</select>
<br>
<input type="number" name="quantity" id="quantity">
<br>
<input type="submit" name="submit" value="submit">
</form><br /> ```
Just simple mistake for simple calculator, you printed "," instead of point(".")
$first = 0,6;
$second = 0,5;
$third = 2,8;

if statement error showing different values

I am newbie to PHP and I am trying to study "if statement" by making calculator and I end up with an error.the result showing is not correct and also please help to suggest how to clear input after each submission.
<html !doctype>
<head>
<title>
</title>
</head>
<body>
<?php
error_reporting();
if(isset($_POST['submit'])) {
$num1 = $_POST['number1'];
$num2 = $_POST['number1'];
$action = $_POST['action1'];
if($action == "addition"){
$add = $num1 + $num2;
echo "your value is";
echo $add;
}
if($action == "subtraction"){
$sub = $num1 - $num2;
echo "your value is";
echo $sub;
}
if($action =="multipilcation"){
$multi = $num1 * $num2;
echo "your value is";
echo $multi;
}
if($action =="division"){
$divi = $num1 / $num2;
echo "your value is";
echo $divi;
}
}
?>
<form method='post' name='myform'>
Enter Number 1:<br>
<input type="number" name="number1" >
<br>
Enter Number 2:<br>
<input type="number" name="number2" >
<br>
choose operation<br>
<select name='action1'>
<option>addition</option>>
<option>subtraction</option>
<option>multipilcation</option>
<option>division</option>
</select>
<input type="submit" name="submit" value="submit">
</form>
</body>
<footer>
</footer>
</html>
You used same number1 for two variables, change it to number2
$num1 = $_POST['number1'];
$num2 = $_POST['number2'];
^
$action = $_POST['action1'];
1) You are not passing any value in <option></option> tag.
<select name='action1'>
<option value="addition">addition</option>>
<option value="subtraction">subtraction</option>
<option value="multipilcation">multipilcation</option>
<option value="division">division</option>
</select>
2) Change $num2 = $_POST['number1']; to $num2 = $_POST['number2'];
<?
if(isset($_POST['submit'])) {
$num1 = $_POST['number1'];
$num2 = $_POST['number2'];
$action = $_POST['action1'];
$num2 = $_POST['number1'];
chnage with:
$num2 = $_POST['number2'];
Also add value attribute for option tag inside select.
<option value="addition">addition</option>>
<option value="subtraction">subtraction</option>
<option value="multipilcation">multipilcation</option>
<option value="division">division</option>

Failure to display $result of a calculation

Here we are calculating a person's ideal weight ($result) based off of their selected gender ($gender) and their inputted height ($height).
For some reason, I am unable to see the echoed $result from the calculation.
Anyone have any clues as to why I cannot see this result?
<?php
if(isset($_POST['Submit'])) {
$gender = $_POST['selectedGender'];
$height = $_POST['patientHeight'];
$result;
if ($gender == "Male") {
$result = ($height * 4) - 128;
echo $result; }
else if ($gender == "Female") {
$result = ($height * 3.5) - 108;
echo $result; }
}
?>
<html>
<div align="center">
<body>
<form name="form" method="post" action="<?php echo $PHP_SELF;?>">
Select Your Gender: <select name="selectedGender">
<option value=""></option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br><br>
Enter Your Height: <input type="number" name="patientHeight" placeholder="Units are in Inches">
<br><br>
<input type="submit" name="Submit" value="Calculate Your Ideal Weight"/>
</form>
</body>
</div>
</html>
Your use of if / else if isn't the best way to do this. You should be using a switch/case call.
switch($gender) {
case "Male":
$result = ($height * 4) - 128;
break;
case "Female":
$result = ($height * 3.5) - 108;
break;
default: // notice the 'default' here?
$result = 'derp'; // that will set it always.
break; // meaning if you don't have a value present.
}
echo $result;
The above will cover all results, including the one where $gender isn't set.
As noted in other answers, there is no such variable called $PHP_SELF, you'll find that in server super global - $_SERVER['PHP_SELF'].
I noticed that there is an empty radio button with the name selectedGender. If this is passed to the form, the if else if statement will not catch it, and not echo anything.
Maybe take out that empty radio button, or add a final else statement.
Your echo is above <html>. This code will calculate the weight and render it in a javascript alert:
<?php
if(isset($_POST['Submit'])) {
$gender = $_POST['selectedGender'];
$height = $_POST['patientHeight'];
$result;
if ($gender == "Male") { $result = ($height * 4) - 128; }
else if ($gender == "Female") { $result = ($height * 3.5) - 108; }
$msg = 'As a '.$gender.' '.$height.' inches tall your weight should be '.$result.' pounds.';
echo '<Script language="javascript">alert("'.$msg.'");"</script>';
}
?>

if elseif php simple calculator

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'])

PHP Calculator: My solutions won't show up I click calculate

I've recently started the assignment of building a calculator out of PHP and I can't seem to find what I'm doing wrong in my code. Every time I press calculate it doesn't give me back my solution.
<?php
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$cal = $_GET['opt'];
if($num2, $num2 != (int)){
$num1=0;
$num2=0;
}
switch($cal) {
case 'add':
echo $num1+$num2;
break;
case 'sub':
echo $num1-$num2;
break;
case 'mul':
echo $num1*$num2;
break;
case 'div':
echo $num1/$num2;
break;
default:
echo "Invalid Operator";
}
?>
Here is the HTML
<form action="calculate.php" method="GET"/>
Number 1:<input type="text" name="num1"/>
<br />
<select>
<option type="text" name="opt" value="add"> + </option>
<option type="text" name="opt" value="sub"> - </option>
<option type="text" name="opt" value="mul"> * </option>
<option type="text" name="opt" value="div"> / </option>
</select>
<br />
Number 2:<input type="text" name="num2"/>
<br />
<input type="submit" value="calculate"/>
</form>
if($num2, $num2 != (int)) looks like a syntax error to me (the comma).
You just say it doesn't work, are you getting an error message? Have you made sure error reporting is on and displaying errors to your browser? I think it should tell you about the syntax error.
try :
$num1 = intval($_GET['num1']);
$num2 = intval($_GET['num2']);
and remove
if($num2, $num2 != (int)){
$num1=0;
$num2=0;
}
I would initialise the var differently:
<?php
$num1 = $num2 = 0;
if (isset($_GET['num1']) && isset($_GET['num1']) && isset($_GET['num1']))
{
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
// edit: added validation
if (!is_numeric($num1) || !is_numeric($num2))
{
$res = NULL;
}
else
{
$cal = $_GET['opt'];
switch($cal)
{
case 'add':
$res = $num1+$num2;
break;
case 'sub':
$res = $num1-$num2;
break;
case 'mul':
$res = $num1*$num2;
break;
case 'div':
$res = $num1/$num2;
break;
default:
$res = NULL;
}
}
}
// display html on the same file
?>
<html>
<body>
<form action="calculate.php" method="GET"/>
Number 1:<input type="text" name="num1"/>
<br />
<select>
<option type="text" name="opt" value="add"> + </option>
<option type="text" name="opt" value="sub"> - </option>
<option type="text" name="opt" value="mul"> * </option>
<option type="text" name="opt" value="div"> / </option>
</select>
<br />
Number 2:<input type="text" name="num2"/>
<br />
<input type="submit" value="calculate"/>
</form>
<? if (isset($res) && $res != NULL): ?>
<span class="result-label">Result:</span> <span class="result"><?=$res?></span>
<? endif ?>
</body>
</html>
This is wrong
if($num2, $num2 != (int)){
$num1=0;
$num2=0;
}
try
$num1 = intval( $num1 );
$num2 = intval( $num2 );

Categories