仅使用php语言的计算器只有一个表单输入

时间:2018-04-09 06:30:44

标签: php html

我正在使用一个输入表单创建一个php计算器。我已经尝试了这么多,但是我无法对我的编码做任何操作,所以我得到了理想的答案。

<?php

if (isset($_POST['select1']) && in_array($_POST['select1'])) {
  $message = "1";
}
if (isset($_POST['select2'])) {
  $message = "2";
}
if (isset($_POST['select3'])) {
  $message = "3";
}
if (isset($_POST['select4'])) {
  $message = "4";
}
if (isset($_POST['select5'])) {
  $message = "5";
}
if (isset($_POST['select6'])) {
  $message = "6";
}
if (isset($_POST['select7'])) {
  $message = "7";
}
if (isset($_POST['select8'])) {
  $message = "8";
}
if (isset($_POST['select9'])) {
  $message = "9";
}
if (isset($_POST['select0'])) {
  $message = "0";
}
if (isset($_POST['select+'])) {
  $message = "+";
}
if (isset($_POST['select-'])) {
  $message = "-";
}
if (isset($_POST['select/'])) {
  $message = "/";
}
if (isset($_POST['select*'])) {
  $message = "*";
}
if (isset($_POST['clear'])) {
  $message = "clear";
}
if (isset($_POST['calculate'])) {
  $message = "";
}


?>

  <div style="padding-left: 200px; margin-top: 100px">
    <form method="post">
      Enter value: <input type="text" name="txt" value="<?php
if (isset($message)) {
  echo $message;
}
?>">
      <div style="padding-left: 110px">

        <input type="submit" name="select1" value="1">
        <input type="submit" name="select2" value="2">
        <input type="submit" name="select3" value="3">
        <input type="submit" name="select4" value="4"><br><br>
        <input type="submit" name="select5" value="5">
        <input type="submit" name="select6" value="6">
        <input type="submit" name="select7" value="7">
        <input type="submit" name="select8" value="8"><br><br>
        <input type="submit" name="select9" value="9">
        <input type="submit" name="select0" value="0">
        <input type="submit" name="select+" value="+">
        <input type="submit" name="select-" value="-"><br><br>
        <input type="submit" name="select/" value="/">
        <input type="submit" name="select*" value="*">
        <input type="submit" name="clear*" value="clear"> <br><br>
        <input type="submit" name="calculate" value="calculate">

      </div>

    </form>
  </div>

以上是我尝试过的代码。请任何人都能告诉我我必须做些什么改动才能使我的所有操作都有效。

1 个答案:

答案 0 :(得分:0)

还有很多其他尝试方法,但我修复了代码中无效的部分。

您的代码中不需要in_array()。此外,您的"clear*"输入中应该是"clear"

您可以将当前$txt捕获到新变量$current_txt中,然后将$message附加到其中。

这不是一种安全的方法,但您可以使用eval()在公式中进行计算。非常简单的代码注入。 txt输入字段中的任何内容都将被评估,这将运行给予它的任何命令。 (尝试在输入框中键入'phpinfo()'并按'计算'。)

通过字符串解析编写函数来进行数学运算并输出结果会好得多。

注意:这不验证公式。如果公式格式错误(例如eval()

"2++3"将失败
<?php
// Capture current formula in input box
if (isset($_POST['txt'])) {
    $current_txt = $_POST['txt'];
}

if (isset($_POST['select1'])) {
  $message = "1";
}
if (isset($_POST['select2'])) {
  $message = "2";
}
if (isset($_POST['select3'])) {
  $message = "3";
}
if (isset($_POST['select4'])) {
  $message = "4";
}
if (isset($_POST['select5'])) {
  $message = "5";
}
if (isset($_POST['select6'])) {
  $message = "6";
}
if (isset($_POST['select7'])) {
  $message = "7";
}
if (isset($_POST['select8'])) {
  $message = "8";
}
if (isset($_POST['select9'])) {
  $message = "9";
}
if (isset($_POST['select0'])) {
  $message = "0";
}
if (isset($_POST['select+'])) {
  $message = "+";
}
if (isset($_POST['select-'])) {
  $message = "-";
}
if (isset($_POST['select/'])) {
  $message = "/";
}
if (isset($_POST['select*'])) {
  $message = "*";
}
if (isset($_POST['clear'])) {
  $message = "clear";
}
if (isset($_POST['calculate'])) {
  $message = "calc";
}

if ($message == "calc") {
    // Calculate using eval 
    $current_txt .= " = " . eval('return ' . $current_txt . ';');
}
else {
    // Append input to formula
    $current_txt .= $message;
}

if ($message == "clear") {
    $current_txt = "";
}
?>

<div style="padding-left: 200px; margin-top: 100px">
  <form  method="post">
    Enter value:  <input type="text" name="txt" value="<?php
    echo $current_txt;
?>" >
    <div style="padding-left: 110px">

      <input type="submit" name="select1" value="1">
      <input type="submit" name="select2" value="2">
      <input type="submit" name="select3" value="3">
      <input type="submit" name="select4" value="4"><br><br>
      <input type="submit" name="select5" value="5">
      <input type="submit" name="select6" value="6">
      <input type="submit" name="select7" value="7">
      <input type="submit" name="select8" value="8"><br><br>
      <input type="submit" name="select9" value="9">
      <input type="submit" name="select0" value="0">
      <input type="submit" name="select+" value="+">
      <input type="submit" name="select-" value="-"><br><br>
      <input type="submit" name="select/" value="/">
      <input type="submit" name="select*" value="*">
      <input type="submit" name="clear" value="clear"> <br><br>
      <input type="submit" name="calculate" value="calculate">

    </div>

  </form>
</div>

编辑: 以下是使用多个同名输入的代码的清洁版本:

<?php
// Capture current formula in input box
if (isset($_POST['txt'])) {
    $current_txt = $_POST['txt'];
}

if (isset($_POST['selectchar'])) {
    $current_txt .= $_POST['selectchar'];
}
if (isset($_POST['clear'])) {
    $current_txt = "";
}
if (isset($_POST['calculate'])) {
    $current_txt .= " = " . eval('return ' . $current_txt . ';');
}
?>

<div style="padding-left: 200px; margin-top: 100px">
    <form  method="post">
        Enter value:  <input type="text" name="txt" value="<?php echo $current_txt; ?>" >
        <div style="padding-left: 110px">
            <input type="submit" name="selectchar" value="1">
            <input type="submit" name="selectchar" value="2">
            <input type="submit" name="selectchar" value="3">
            <input type="submit" name="selectchar" value="4"><br><br>
            <input type="submit" name="selectchar" value="5">
            <input type="submit" name="selectchar" value="6">
            <input type="submit" name="selectchar" value="7">
            <input type="submit" name="selectchar" value="8"><br><br>
            <input type="submit" name="selectchar" value="9">
            <input type="submit" name="selectchar" value="0">
            <input type="submit" name="selectchar" value="+">
            <input type="submit" name="selectchar" value="-"><br><br>
            <input type="submit" name="selectchar" value="/">
            <input type="submit" name="selectchar" value="*">
            <input type="submit" name="clear" value="clear"> <br><br>
            <input type="submit" name="calculate" value="calculate">
        </div>
    </form>
</div>