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 );
Related
I am doing PHP practice and I am stuck on this part.
My problem is when I do simple math like 1+1 or 1/1 etc. The $result won't display. I am confused about which part of my code is causing the problem. Please enlighten me on my mistakes
Thank you.
<?php
$first_num = $_POST['first_num'];
$second_num = $_POST['second_num'];
$operator = $_POST['operator'];
$result = '';
function cal_result($first_num,$second_num,$operator){
if (is_numeric($first_num) && is_numeric($second_num)) {
switch ($operator) {
case "Add":
$result = $first_num + $second_num;
break;
case "Subtract":
$result = $first_num - $second_num;
break;
case "Multiply":
$result = $first_num * $second_num;
break;
case "Divide":
$result = $first_num / $second_num;
}
return $result;
}
}
?>
<form action="simple_calculator.php" method="post" id="quiz-form">
<p>
<input type="number" name="first_num" id="first_num" required="required" value="<?php cal_result($first_num,$second_num,$operator); ?>" /> <b>First Number</b>
</p>
<p>
<input type="number" name="second_num" id="second_num" required="required" value="<?php cal_result($first_num,$second_num,$operator); ?>" /> <b>Second Number</b>
</p>
<p>
<input readonly="readonly" name="result" value="<?php echo $result; ?>"> <b>Result</b>
</p>
<input type="submit" name="operator" value="Add" />
<input type="submit" name="operator" value="Subtract" />
<input type="submit" name="operator" value="Multiply" />
<input type="submit" name="operator" value="Divide" />
</form>
I think you are confused by the short open tag
if I print
<?=$result;?>
or
<?=cal_result($first_num,$second_num,$operator);?>
this is the same thing like
<?php echo $result;?>
or
<?php echo cal_result($first_num,$second_num,$operator);?>
If you call a function that ´returns´ values, and you want to use them , you must also print them out...
also check out global variables. If you want to use $result outside the function scope you must declare it a global variable.
<?php
$result = '';
$first_num = 0;
$second_num = 0;
$ops = array( "+",
"-",
"×", // HTML : × , ASCII CODE : 158
"÷"); // HTML : ÷ , ASCII CODE : 246
// checking if the first number is what he should be
if ( isset($_POST['first_num']) &&
!empty($_POST['first_num']) &&
is_numeric($_POST['first_num']) )
{
$first_num = $_POST['first_num'];
}
// checking if the second number is what he should be
if ( isset($_POST['second_num']) &&
!empty($_POST['second_num']) &&
is_numeric($_POST['second_num']) )
{
$second_num = $_POST['first_num'];
}
function cal_result($first_num,$second_num,$operator){
// optionally you can assign global
global $result;
// here we prevent injection by acceptint only operators from the ops array
if (isset($_GET['operator']) && in_array($_GET['operator'],$ops)) {
switch ($_GET['operator']) {
case $ops[0]: // first element of array ops is Add
$result = $first_num + $second_num;
// remember this function is just returning the $result
// variable it is not printing it, you will have to assiging
// the function to a variable if you want to use the
return $result;
break;
case $ops[1]: // second element of array ops is Subtract
$result = $first_num - $second_num;
return $result;
break;
case $ops[2]: // third element of array ops is Multiply
$result = $first_num * $second_num;
return $result;
break;
case $ops[3]: // fourth element of array ops is Divide
// divide by zero problem
if($second_num !== 0){
$result = $first_num / $second_num;
}else{
$result = "Err: Div By Zero";
}
return $result;
break;
}
}
}
}
?>
<p>
<!--// now you have 2 possibilities : //-->
<!--// first you can call the global variable $result //-->
<label for="quiz-form"><?=$result;?></label>
<!--// the second possibility is to call the function //-->
<label for="quiz-form"><?=cal_result($first_num,$second_num,$operator);?></label>
<b>Result</b>
</p>
<!-- for post you should also set enctype for security reasons -->
<form action="simple_calculator.php"
method="post"
id="quiz-form"
enctype="application/x-www-form-urlencoded">
<p>
<input type="number"
name="first_num"
id="first_num"
required="required"
value="<?=$first_num;?>" />
<b>First Number</b>
</p>
<p>
<input type="number"
name="second_num"
id="second_num"
required="required"
value="<?=$second_num;?>" />
<b>Second Number</b>
</p>
<input type="submit" name="operator" value="+" alt="Add" />
<input type="submit" name="operator" value="-" alt="Subtract" />
<input type="submit" name="operator" value="×" alt="Multiply" />
<input type="submit" name="operator" value="÷" alt="Divide" />
</form>
The code will not produce a calculated result for me.. can anyone help me?
The HTML displays properly, I can input the constants, they are updated in the URL. The PHP GET function seems to be working but not producing any results on the page.
<form>
<input type="text" name="num1" placeholder="Constant">
<input type="text" name="num2" placeholder="Constant">
<select name="Operator">
<option>Select Option</option>
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<br>
<br>
<button type="Submit" name="Submit" value="Submit">Calculate</button>
</form>
<p>The answer is: </p>
if (isset($_GET['Submit'])) {
$result1 = $_GET['num1'];
$result2 = $_GET['num2'];
$operator = $_GET['Operator'];
switch ($operator) {
case 'Select Option':
echo "Error";
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;
}}
Here is my form
<form>
<input type="text" name="num1" placeholder="Digets">
<input type="text" name="num2" placeholder="Digets">
<select name="operator">
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_GET['$submit'])){
$result1 = $_GET['$num1'];
$result2 = $_GET['$num2'];
$operator = $_GET['$operator'];
switch ('$operator'){
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
When i click on the calculate button I don't see an answer and i tried fixing all my bugs but it seems to me as if their is something wrong with the switch.
Try this:-
you are making some mistake while getting values and switch parameter.
like you are geetting value $_GET['$num1'] instead you need to use $_GET['num1']
as you just need to pass variable switch ($operator) instead switch ('$operator') of quotation mark.
<?php
if (isset($_GET['submit'])){
$result1 = $_GET['num1'];
$result2 = $_GET['num2'];
$operator = $_GET['operator'];
switch ($operator){
case 'Add':
$FResult =$result1 + $result2;
echo "Result:"+ $FResult ;
break;
case 'Subtract':
$FResult =$result1 - $result2;
echo "Result:"+ $FResult ;
break;
case 'Multiply':
$FResult =$result1 * $result2;
echo "Result:"+ $FResult ;
break;
case 'Divide':
$FResult =$result1 / $result2;
echo "Result:"+ $FResult ;
break;
default :
echo "wrong";
}
}
?>
The mistake you have made:
1). You are passing variable directly into get element i.e, it should be like $_GET['submit'] instead of $_GET['$submit'];
2). You are passing string instead of variable i.e, switch ('$operator') should be switch ($operator)
check it out where you have made mistake
<form method="GET" action="">
<input type="text" name="num1" placeholder="Digets">
<input type="text" name="num2" placeholder="Digets">
<select name="operator">
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_GET['submit'])){
$result1 = $_GET['num1'];
$result2 = $_GET['num2'];
$operator = $_GET['operator'];
switch ($operator){
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
You were looking for PHP solution, here is one:-
<?php
if (isset($_POST['submit'])){
$result1 = $_POST['num1'];
$result2 = $_POST['num2'];
$operator = $_POST['operator'];
switch($operator){
case "Add":
$res = +$result1 + +$result2;
break;
case "Subtract":
$res = +$result1 - +$result2;
break;
case "Multiply":
$res = +$result1 * +$result2;
break;
case "Divide":
$res = +$result1 / +$result2;
break;
}
}
?>
<form method="post" action="">
<input type="text" name="num1" id="num1" placeholder="Digets">
<input type="text" name="num2" id="num2" placeholder="Digets">
<select name="operator" id="operator">
<option value="Add">Add</option>
<option value="Subtract">Subtract</option>
<option value="Multiply">Multiply</option>
<option value="Divide">Divide</option>
</select>
<input type="submit" name="submit" id="submit" value="Calculate" />
<p>Your answer is : <input name="res" value="<?php echo $res; ?>"></p>
</form>
This is what you were looking for .
<form>
<input type="number" name="num1" placeholder="Digets">
<input type="number" name="num2" placeholder="Digets">
<select name="operator">
<option value="+">Add</option>
<option value="-">Subtract</option>
<option value="*">Multiply</option>
<option value="/">Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_REQUEST['submit'])){
$result1 = $_REQUEST['num1'];
$result2 = $_REQUEST['num2'];
$operator = $_REQUEST['operator'];
$res = $result1.$operator.$result2;
$p = eval('return '.$res.';');
print $p;
}
?>
<form>
<input type="number" name="num1" placeholder="Digets">
<input type="number" name="num2" placeholder="Digets">
<select name="operator">
<option>Add</option>
<option>Subtract</option>
<option>Multiply</option>
<option>Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_REQUEST['submit'])){
$result1 = $_REQUEST['num1'];
$result2 = $_REQUEST['num2'];
$operator = $_REQUEST['operator'];
switch ($operator){
case "Add":
echo $result1 + $result2;
break;
case "Subtract":
echo $result1 - $result2;
break;
case "Multiply":
echo $result1 * $result2;
break;
case "Divide":
echo $result1 / $result2;
break;
}
}
?>
Another way:
<form>
<input type="number" name="num1" placeholder="Digets">
<input type="number" name="num2" placeholder="Digets">
<select name="operator">
<option value="+">Add</option>
<option value="-">Subtract</option>
<option value="*">Multiply</option>
<option value="/">Divide</option>
</select>
<button type="submit" value="submit" name="submit">Check for the answer</button>
<p>Your answer is : </p>
</form>
<?php
if (isset($_REQUEST['submit'])){
$result1 = $_REQUEST['num1'];
$result2 = $_REQUEST['num2'];
$operator = $_REQUEST['operator'];
$res = $result1.$operator.$result2;
$p = eval('return '.$res.';');
print $p;
}
?>
You should set option value in select tag (operator which you want for calculator) <option value="">.
change you input type="text" into type="number"
<form method="GET">
<input type="number" name="num1">
<input type="number" name="num2">
<select name="operator">
<option value='+'>Add</option>
<option value='-'>Subtract</option>
<option value='*'>Multiply</option>
<option value='/'>Divide</option>
</select>
<button type="submit" value="submit" name="submit">calculate</button>
<p>Answer :</p>
</form>
<?php
if (isset($_GET['submit'])){
$num1 = $_GET['num1'];
$num2 = $_GET['num2'];
$operator = $_GET['operator'];
switch ($operator){
case "+":
echo $num1 + $num2;
break;
case "-":
echo $num1 - $num2;
break;
case "*":
echo $num1 * $num2;
break;
case "/":
echo $num1 / $num2;
break;
}
}
?>
what you have done is that you are calculating it in the back-end and the form is refreshing the page , but not displaying the result anywhere on the page . Its better to do with a JQuery so that result will come instantly without page refresh . Just check the code below:-
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$("#submit").on('click', function(){
var num1 = $("#num1").val() ;
var num2 = $("#num2").val() ;
var operator = $("#operator").val() ;
var result = 0 ;
switch(operator){
case "Add":
result = +num1 + +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
case "Subtract":
result = +num1 - +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
case "Multiply":
result = +num1 * +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
case "Divide":
result = +num1 / +num2 ;
document.getElementById("res").innerHTML = result ;
break ;
}
});
});
</script>
<div>
<input type="text" name="num1" id="num1" placeholder="Digets">
<input type="text" name="num2" id="num2" placeholder="Digets">
<select name="operator" id="operator">
<option value="Add">Add</option>
<option value="Subtract">Subtract</option>
<option value="Multiply">Multiply</option>
<option value="Divide">Divide</option>
</select>
<button name="submit" id="submit">Check for the answer</button>
<p>Your answer is : <span id="res"></span></p>
</div>
This is running successfully .
I want to display results of the calculation at the end of the program as a list. After each post, the result is added to the list of previous results.
Simply, I want to display a history of the result of the calculations done.
<form method="post" >
<p>A:<input value="<?php echo $_POST['num1']?>" min="0" max="1000" type="number" name="num1"></p>
<p>B:<input value="<?php echo $_POST['num2']?>" min="0" max="1000" type="number" name="num2"></p>
<p>
<select name="op">
<option value="jaam">+</option>
<option value="zarb">*</option>
<option value="taghtsim">/</option>
<option value="tafrigh">-</option>
</select>
</p>
<input type="submit" name="">
<hr>
</form>
<?php
if (isset($_POST['num1']) && isset($_POST['num2']) && isset($_POST['op']))
{
switch ($_POST['op'])
{
case 'jaam':
$r = $_POST['num1'] + $_POST['num2'];
break;
case 'zarb':
$r = $_POST['num1'] * $_POST['num2'];
break;
case 'taghtsim':
$r = $_POST['num1'] / $_POST['num2'];
break;
case 'tafrigh':
$r = $_POST['num1'] - $_POST['num2'];
break;
default:
$r = "Error!";
break;
}
}
?>
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>