【问题标题】:Simple Calculator using radio button in php使用 php 中的单选按钮的简单计算器
【发布时间】:2021-10-08 09:30:51
【问题描述】:

我做了一个简单的计算器。但问题不是单选按钮无法正常工作以选择要执行的操作我的输入代码如下:

<form action="output.php" method="post" class="form">
    <label for="">Value 1</label>
    <input type="text" name="value1" placeholder="Enter 1st value">

    <br>
    <label for="">Value 2</label>
    <input type="text" name="value2" placeholder="Enter 2nd value">

    <h1>Select Operator </h1>
    <input type="radio" name="addition"       value="add">+
    <br>
    <input type="radio" name="subtraction"    value="sub">-
    <br>
    <input type="radio" name="multiplication" value="mul">*
    <br>
    <input type="radio" name="division"       value="div">/
    <br>

    <input type="submit" class="btn btn-success" value="Show Result">
</form>

输出php代码如下:

<?php

    $value_1=$_POST['value1'];
    $value_2=$_POST['value2'];
    
    if(isset($_POST['submit'])) {
        if($_POST['operation'] == add) {
            echo "$value_1 + $value_2";
        }else if($_POST['operation'] == sub){
            echo "$value_1 - $value_2";
        }else if($_POST['operation'] == mul){
            echo "$value_1 * $value_2";
        }else if($_POST['operation'] == div){
            echo "$value_1 / $value_2";
        }
    }
?>

【问题讨论】:

  • 您是否将addsubmuldiv 定义为常量?
  • 是的,我将它们添加为常量
  • 您的计算器无法正常工作的原因仅仅是因为您正在调用if( isset( $_POST['submit'] ) ),但实际的submit 按钮没有名称...

标签: php radio-button calculator


【解决方案1】:

submit 按钮没有名称 - 因此它不会出现在 POST 数组中,并且永远不会处理逻辑。与其测试 POST 数组中按钮的存在(或不存在),不如测试您将在计算中使用的实际表单元素。

<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>simple calculator</title>
    </head>
    <body>
        <form method="post" class="form">

            <label>Value 1 <input type="text" name="value1" placeholder="Enter 1st value"></label>
            <label>Value 2<input type="text" name="value2" placeholder="Enter 2nd value"></label>
            

            <h1>Select Operator </h1>
            <input type="radio" name="operator" value="add">+
            <br>
            <input type="radio" name="operator" value="sub">-
            <br>
            <input type="radio" name="operator" value="mul">*
            <br>
            <input type="radio" name="operator" value="div">/
            <br>

            <input type="submit" class="btn btn-success" value="Show Result">
        </form>
        <?php

            /*
                Ensure that ALL post fields are set
            */
            if( isset(
                $_POST['value1'],
                $_POST['value2'],
                $_POST['operator']
            )) {
                /*
                    Assign POST values as variables.
                */
                $value_1=(int)$_POST['value1'];
                $value_2=(int)$_POST['value2'];     
                $operator=$_POST['operator'];
                
                /*
                    Perform the relevant calculation
                    and assign the correct symbol for display
                */
                switch( $operator ){
                    case 'add':$symbol='+';$res=$value_1 + $value_2;break;
                    case 'sub':$symbol='-';$res=$value_1 - $value_2;break;
                    case 'mul':$symbol='*';$res=$value_1 * $value_2;break;
                    case 'div':$symbol='/';$res=$value_1 / $value_2;break;
                }
                
                /*
                    print the sum and result
                */
                printf(
                    '%d %s %s=%d',
                    $value_1,
                    $symbol,
                    $value_2,
                    $res
                );
            }
        ?>
    </body>
</html>

【讨论】:

  • 致命错误:未捕获的错误:C:\xampp\htdocs\ez\output.php:10 中未定义的常量“add” 堆栈跟踪:#0 {main} 在 C:\xampp\htdocs 中抛出\ez\output.php 在第 10 行
  • 错误必须在您对上述内容的翻译/改编中 - 我的示例中没有错误
  • 非常感谢,我解决了忘记在这里使用双引号 ="add" 现在计算器工作正常
【解决方案2】:

单选按钮需要共享相同的名称,只有值应该改变。

label {
  display: block;
}
<label><input type="radio" name="operation" value="add">+</label>
<label><input type="radio" name="operation" value="sub">-</label>
<label><input type="radio" name="operation" value="mul">*</label>
<label><input type="radio" name="operation" value="div">/</label>

【讨论】:

  • 谢谢,但没有任何操作仍然有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-15
相关资源
最近更新 更多