从按钮发送值到文本框

时间:2014-04-22 15:58:50

标签: php html5

我正在使用PHP制作一个基本的计算器发送给我的techer我在同一个文件中有基本的html和php代码,但我找不到从任何按钮发送数字到文本的方式来制作更大的数字,比如Windows calc,如果我从我的键盘引入数字它确定并且工作但我不知道如何从buttton发送值 这是代码

<html> 

 <head> 

  <title>Calculadora Basica</title> 
 </head> 

 <body> 
  <div align="center"> 
    <center> 
      <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>
      <table border="0" cellspacing="1" width="304" height="139"> 
        <tr> 
          <td width="292" align="center" height="39"> 
            <h3> Calculadora Basica</h3> 
          </td> 
        </tr> 
        <tr> 
          <td width="292" align="center" height="23"> 
            <input type="text" name="num1" size="10"><br>    
            <input type="text" name="num2" size="10"></td> 
        </tr> 
        <tr> 
          <td width="292" align="center" height="24"> 
            <input type="submit" value="+" name="operac"> 
            <input type="submit" value="-" name="operac"> 
            <input type="submit" value="*" name="operac"> 
            <input type="submit" value="/" name="operac"><br> 
            <input type="submit" value="^2" name="operac"> 
            <input type="submit" value="^3" name="operac"> 
            <input type="submit" value="Raiz" name="operac">  
            <input type="submit" value="%" name="operac"><br> 
            <input type="button" value="1" name="1">
            <input type="button" value="2" name="2">
            <input type="button" value="3" name="3"><br> 
            <input type="button" value="4" name="4">
            <input type="button" value="5" name="5">
            <input type="button" value="6" name="6"><br> 
            <input type="button" value="7" name="7">
            <input type="button" value="8" name="8">
            <input type="button" value="9" name="9">
            <input type="button" value="0" name="0">
          </td> 
        </tr> 

        <?php 
            $operac = $_POST['operac'];
            $num1 = doubleval($_POST['num1']);
            $num2 = doubleval($_POST['num2']);

            switch($operac) {
                case "+": $resultado = "{$num1} + {$num2} = ". strval($num1+$num2); break;
                case "*": $resultado = "{$num1} * {$num2} = " . $num1*$num2; break;
                case "/": $resultado = "{$num1} / {$num2} = " . $num1/$num2; break;
                case "-": $resultado = "{$num1} - {$num2} = " . strval($num1-$num2); break;
                case "Raiz": $resultado = "Raiz({$num1}) = " . sqrt($num1); break;
                case "^2": $resultado = "{$num1}^2 = " . $num1*$num1; break;
                case "^3": $resultado = "{$num1}^3 = " . $num1*$num1*$num1; break;
              case "%": $resultado = "{$num2}% de {$num1} = " . (($num2 * 100) / $num1); break;
            }
        ?> 

        <tr> 
          <td width="292" align="center" height="19"><br>
          <b>Resultado: <?php echo $resultado; ?></b></td> 
        </tr>      
      </table> 
     </form> 
   </center> 
  </div> 
</body> 

</html>

1 个答案:

答案 0 :(得分:0)

您必须按如下方式更新您的html代码,以便将点击的数字按钮的值存储到文本框中。在这里,我将该值存储在两个文本框中。 我替换了一些文本框,数字按钮的代码,并添加了一个javascript函数,而不是所有代码都是原样。 请一看。

文本框:

<tr>
<td width="292" align="center" height="23"> 
<input type="text" name="num1" id="num1" value="" size="10">
<input type="text" name="num2" id="num2" value="" size="10"></td> 
</tr> 

按钮:

<input type="button" onclick="storeNumber(this)" value="1" name="1">
<input type="button" onclick="storeNumber(this)" value="2" name="2">
<input type="button" onclick="storeNumber(this)" value="3" name="3"><br> 
<input type="button" onclick="storeNumber(this)" value="4" name="4">
<input type="button" onclick="storeNumber(this)" value="5" name="5">
<input type="button" onclick="storeNumber(this)" value="6" name="6"><br> 
<input type="button" onclick="storeNumber(this)" value="7" name="7">
<input type="button" onclick="storeNumber(this)" value="8" name="8">
<input type="button" onclick="storeNumber(this)" value="9" name="9">
<input type="button" onclick="storeNumber(this)" value="0" name="0">

在头标记中调用Javascript函数

<script language='javascript'>
function storeNumber( obj )
{       
var previousVal = document.getElementById("num1").value;
previousVal = previousVal + obj.value;
document.getElementById("num1").value = previousVal;
document.getElementById("num2").value = previousVal;
 }  
 </script>

感谢。