本文目錄一覽:
如何用PHP做的計算器嗎
?php/**
* Created by PhpStorm.
* User: ITAK
* Date: 2017/3/3
* Time: 10:28
*/
error_reporting(E_ALL ~E_NOTICE); if(isset($_POST[‘submit’])){ $ok = true; $error = “出現的問題:br”; if($_POST[‘num1’] == “”){ $ok = false; $error = $error.”第一個數字不能為空br”;
} else{ if(!is_numeric($_POST[‘num1’])){ $ok = false; $error = $error.”第一個數字不是數字br”;
}
} if($_POST[‘num2’] == “”){ $ok = false; $error = $error.”第二個數字不能為空br”;
} else{ if(!is_numeric($_POST[‘num2’])){ $ok = false; $error = $error.”第二個數字不是數字br”;
}
}
} if($ok){ $sum = “”; $fuhao = $_POST[‘fuhao’]; if($fuhao == ‘+’) $sum = $_POST[‘num1’] + $_POST[‘num2’]; if($fuhao == ‘-‘) $sum = $_POST[‘num1’] – $_POST[‘num2’]; if($fuhao == ‘*’) $sum = $_POST[‘num1’] * $_POST[‘num2’]; if($fuhao == ‘/’) $sum = $_POST[‘num1’] / $_POST[‘num2’]; if($fuhao == ‘%’) $sum = $_POST[‘num1’] % $_POST[‘num2’];
} echo “br”;?html
head
meta charset=”UTF-8″
title簡單計算器/title
/head
body
table border=”0″ width=”400″ align=”center”
form action=”cal.php” method=”post”
captionh1簡單計算器/h1/caption
tr
td
input type=”text” size=”5″ name=”num1″ value=”?php echo $_POST[‘num1’] ?”/
/td
td
select name=”fuhao”//下拉列表 option ?php if($_POST[‘fuhao’]==”+”) echo “selected”?
value=”+” + /option
option ?php if($_POST[‘fuhao’]==”-“) echo “selected”?
value=”-” – /option
option ?php if($_POST[‘fuhao’]==”*”) echo “selected”?
value=”*” * /option
option ?php if($_POST[‘fuhao’]==”/”) echo “selected”?
value=”/” / /option
option ?php if($_POST[‘fuhao’]==”%”) echo “selected”?
value=”%” % /option
/select
/td
td
input type=”text” name=”num2″ size=”5″ value=”?php echo $_POST[‘num2’] ?”/
/td
td
= /td
td
input type=”text” name=”res” size=”5″ value=”?php echo $sum ?”/
/td
/tr
tr align=”center”
td
input type=”submit” value=”計算” name=”submit”
/td
/tr
br
tr
td colspan=”4″
?php
if($ok){ echo “結果為: {$_POST[‘num1’]} {$_POST[‘fuhao’]} {$_POST[‘num2’]} = {$sum}”;} else{ echo $error;} ?
/td
/tr
/form
/table
/body/html
求解答php簡單計算器代碼
朋友,你這段代碼其實問題很多:
手誤: switch ($_POST[‘Submint’])
邏輯錯誤: if($_POST[‘txt_num1’]!=null $_POST[‘txt_num2’]!=null)
因為在沒有提交的情況下,變數:$_POST[‘txt_num1’]和$_POST[‘txt_num2’]是不存在的
想法錯誤:
switch ($_POST[‘Submint’])
{
case “+”: $num3=$num1 + $num2;break;
case “-“: $num3=$num1-$num2;break;
case “*”: $num3=$num1*$num2;break;
case “/”: $num3=$num1/$num2;break;
default:break;
}
原因:你可以列印出變數看看:print_r($_POST)就知道問題所在了
4. 考慮不周:input type=”text” name=”txt_num3″ value=”?php echo $num3php?”/在沒有提交的情況下,你怎麼來的變數:$num3php,即使有,也是 $num3
方法:可以結合jquery來判斷用戶單擊的是哪個submit,然後再提交給php來出來並返回結果。具體細節您自己學習吧,憑你現在寫出來的代碼,你還需要一步一步來。
用php做個計算器(加減乘除),兩個文本框輸入數字,第三個輸出結果並
不需要php呀
這樣寫的行不
!DOCTYPE html
html
head
title簡單計算器/title
/head
body
input type=”text” name=”first” id=”first”
select id=”operate”
option+/option
option-/option
option*/option
option//option
/select
input type=”text” name=”second” id=”second”=
input type=”text” name=”result” id=”result”
input type=”button” name=”運算” value=”運算” onClick=”operate()”
script type=”text/javascript”
function operate() {
var first = parseInt(document.getElementById(“first”).value);
var second = parseInt(document.getElementById(“second”).value);
var result = document.getElementById(“result”);
var opt = document.getElementById(“operate”);
if (0 == opt.selectedIndex) {
resultvalue = first + second;
}else if(1 == opt.selectedIndex){
resultvalue = first – second;
}else if (2 == opt.selectedIndex) {
resultvalue = first * second;
}else if (3 == opt.selectedIndex) {
if (second == 0) {
alert(“除數不能為0”);
}
resultvalue = first / second;
}
result.setAttribute(“value”,resultvalue);
}
/script
/body
/html
各位童鞋,請問PHP中做簡單計算器
oper應該是operate的縮寫吧,可能是用列表,或者單選框的形式來選擇運算符,$_POST是用來獲取POST方式提交表單的數據,$_POST[“oper”]是表單裡面,名稱為”oper”的數據,名稱通常用具體的意義命名的。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/244350.html