if statement error showing different values - php

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>

Related

Focus on HTML Select dropdown option after GET request

So I am making a programme and I am submitting a form to itself. This programme is a very basic calculator as I am new to HTML and PHP. I am trying to make it so that when you submit the form, the Select dropdown will remain on the most recently used operator.
For example, if I make the calculator do '5 + 5', then I want the submitted form to keep the operator dropdown on '+'.
Here is my code:
<?php
// grab the form values from $_HTTP_GET_VARS hash extract($_GET);
// first compute the output, but only if data has been input if(isset($calc) && $operator == "multiply") { // $calc exists as a variable
$prod = $x * $y; } elseif (isset($calc) && $operator == "plus") {
$operator = $plus;
$prod = $x + $y; } elseif (isset($calc) && $operator == "minus") {
$operator = "minus";
$prod = $x - $y; } elseif (isset($calc) && $operator == "divide") {
$sign = "/";
$prod = $x / $y; } else { // set defaults
$x=0;
$y=0;
$prod=0; } ?>
<html> <head>
<title>PHP Calculator Example</title> </head>
<body>
<h3>PHP Calculator (Version 1)</h3>
<p>Multiply two numbers and output the result</p>
<form method="get" action="<?php print $_SERVER['PHP_SELF']; ?>">
<label for="x">x = </label>
<input type="text" name="x" id="x" size="5" value="<?php print $x; ?>"/>
<select name="operator" id="operator">
<option value="plus">+</option>
<option value="minus">-</option>
<option value="divide">/</option>
<option value="multiply">*</option>
</select>
<label for="y">y = </label>
<input type="text" name="y" id="y" size="5" value="<?php print $y; ?>"/>
<input type="submit" name="calc" value="Calculate"/>
</form>
<!-- print the result -->
<?php if(isset($calc)) {
print "<p>x $sign y = $prod</p>";
} ?>
</body> </html>
In order to remain on option selected in the select element
The option has to have an attribute "selected"
<select name="operator" id="operator">
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "plus"){echo "selected";} ?> value="plus">+</option>
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "minus"){echo "selected";} ?> value="minus">-</option>
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "divide"){echo "selected";} ?> value="divide">/</option>
<option <?php if(isset($_GET['operator']) && $_GET['operator'] == "multiply"){echo "selected";} ?> value="multiply">*</option>
</select>
Hope that it will help you.
A possible solution employing javascript would be this
<?php
if(isset($_GET['operator'])) {
?>
<script>document.getElementById("operator").value = '<?=$_GET['operator']?>';</script>
<?php
}
?>

The $GLOBALS do not get the expected data

I have the bellow php code:
<form method="get">
<input type="text" name="num1" placeholder="num1">
<input type="text" name="num2" placeholder="num2">
<button type="submit" name="submit" value="func1">Submit</button>
</form>
The result is: <?php
if (isset($GLOBALS['result'])) {
echo $GLOBALS['result']; // there do not get the add result.
}
?>
<?php
$GLOBALS['result'] = 0;
if(isset($_GET['submit']) && $_GET['submit'] == 'func1'){
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$GLOBALS['result'] = $num1 + $num2;
}
?>
The issue is if (isset($GLOBALS['result'])) {...} the code did not walk through.
Where is my issue?
Or how can I use a variables in different php tags?
Try below code
It is working.
<?php
session_start();
?>
<form method="get">
<input type="text" name="num1" placeholder="num1">
<input type="text" name="num2" placeholder="num2">
<button type="submit" name="submit" value="func1">Submit</button>
</form>
<?php
if(isset($_GET['submit']) && $_GET['submit'] == 'func1'){
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$_SESSION['result'] = ($num1 + $num2);
}
?>
The result is: <?php
if (isset($_SESSION['result'])) {
echo $_SESSION['result']; // there do not get the add result.
}
?>
The page gets refreshed after submitting the form.
So the value get reset in GLOBALS.
If you can echo GLOBALS inside the submit condition then you will get the values.
Note : Use SESSION instead of GLOBALS.
<?php
if(isset($_GET['submit']) && $_GET['submit'] == 'func1'){
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$GLOBALS['result'] = $num1 + $num2;
echo $GLOBALS['result'];
}
?>

PHP unable to write a variable to XML file

I am unable to get my php code to write my $result variable into the XML file. It does write the $number1 variable that I just added for testing but it doesnt want to add the $result. I need this, so that I can make a memory and memory recall button for my calculator. This is for a school project and I have to use XML. Thanks.
<?php
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];;
$operator = $_POST['operator'];
if ($operator == 'subtract'){
subtract();
}
if ($operator == 'add'){
add();
}
if ($operator == 'multiply'){
multiply();
}
if ($operator == 'divide'){
divide();
}
function add() {
global $number1;
global $number2;
$result = $number1 + $number2;
echo $result;
}
function subtract() {
global $number1;
global $number2;
$result = $number1-$number2;
echo $result;
}
function multiply() {
global $number1;
global $number2;
$result = $number1*$number2;
echo $result;
}
function divide() {
global $number1;
global $number2;
$result = $number1/$number2;
echo $result;
}
$handle = fopen('Data.xml', 'w');
fwrite($handle, "<inputs>\n\t<input>\n\t\t<answer>".$result."</answer>\n\t\t<number1>".$number1."</number1>\n\t</input>\n</inputs>");
fclose($handle);
echo "Write to input.xml successfully";
?>
<html>
<center>
<h1 style="color:blue;">Rohit's Math Machine</h1>
<form action="Code.php" method="POST">
Enter Number 1: <input type="text" name="number1" method="POST"><br><br>
<select name="operator" method="POST">
<option value="subtract">subtract</option>
<option value="add">add</option>
<option value="multiply">multiply</option>
<option value="divide">divide</option>
</select><br><br>
Enter Number 2: <input type="text" name="number2" method="POST"><br><br>
<input type="submit" value="Submit">
</form>
</center>
</html>

Calculator selection stays after submit

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

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