我想用php制作计算器,我写了这段代码但是没有解决问题是什么?我使用iclude来附加函数并使用select和option标记并给出选项名称来获取数据。我使用phpstorm来解决问题,但没有问题。抱歉我的英文不好
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form style="margin:25%;" action="#" method="GET">
<input type="text" name="num1" size="20" placeholder="number 1">
<input type="text" name="num2" size="20" placeholder="number 2">
<select name="massoud">
<option name="jam1">jam</option>
<option name="tafrigh1">tafrigh</option>
<option name="zarb1">zarb</option>
<option name="taghsim1">taghsim</option>
<option name="baghimande1">baghimande</option>
</select>
<input type="submit" name="submit" value="calculate">
</form>
<?php
include 'calib.php';
?>
<?php
if(isset($_GET['submit']) && isset($_GET['jam1'])) {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo jam1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['tafrigh1'])) {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo tafrigh1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['zarb1'])) {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo zarb1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['taghsim1'])) {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo taghsim1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['baghimande1'])) {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo baghimande1($a1, $b1);
}
?>
</body>
</html>
这是包含文件
<?php
function jam1($a1, $b1){
return $a1 + $b1;
}
function tafrigh1($a1, $b1){
return $a1 - $b1;
}
function zarb1($a1, $b1){
return $a1 * $b1;
}
function taghsim1($a1, $b1){
return $a1 / $b1;
}
function baghimande1($a1, $b1){
return $a1 % $b1;
}
?>
这是结果网址 http://localhost/calib/?num1=6&num2=5&massoud=tafrigh&submit=calculate#
答案 0 :(得分:1)
在每种情况下,您都要求选项的确切值,而不是选择的值,这是发布的内容:
if(isset($_GET['submit']) && isset($_GET['jam1'])) {
没有$_GET['jam1']
,您应该将其更改为您的选择名称:
if(isset($_GET['submit']) && ($_GET['massoud'] == 'jam1')) {
您需要将其添加到每个案例中
编辑:我没注意到您的选择也设置错误:
<select name="massoud">
<option name="jam1">jam</option>
<option name="tafrigh1">tafrigh</option>
<option name="zarb1">zarb</option>
<option name="taghsim1">taghsim</option>
<option name="baghimande1">baghimande</option>
</select>
您正在设置每个选项的名称而不是值,请更改为:
<select name="massoud">
<option value="jam1">jam</option>
<option value="tafrigh1">tafrigh</option>
<option value="zarb1">zarb</option>
<option value="taghsim1">taghsim</option>
<option value="baghimande1">baghimande</option>
</select>
答案 1 :(得分:1)
更改此行
if(isset($_GET['submit']) && isset($_GET['jam1'])) {
到此:
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == 'jam1') {
jam1是值而不是键。 massoud是选择下拉列表的名称,因此是密钥。
答案 2 :(得分:0)
应该是:
<select name="massoud">
<option value="jam1">jam</option>
<option value="tafrigh1">tafrigh</option>
<option value="zarb1">zarb</option>
<option value="taghsim1">taghsim</option>
<option value="baghimande1">baghimande</option>
</select>
使用$_GET['massoud']
,您将获得所选的值。
答案 3 :(得分:0)
将您的代码更改为此,您将在左下角获得输出。
<?php
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == "jam") {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo jam1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == "tafrigh") {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo tafrigh1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == "zarb") {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo zarb1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == "taghsim") {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo taghsim1($a1, $b1);
}
if(isset($_GET['submit']) && isset($_GET['massoud']) && $_GET['massoud'] == "baghimande1") {
$a1 = $_GET['num1'];
$b1 = $_GET['num2'];
echo baghimande1($a1, $b1);
}
?>