Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将值从按钮发送到文本框_Php_Html - Fatal编程技术网

Php 将值从按钮发送到文本框

Php 将值从按钮发送到文本框,php,html,Php,Html,我用PHP制作了一个基本的计算器发送给我的技术人员我有基本的html和PHP代码在同一个文件中,但我找不到方法将任何按钮的数字发送到文本以生成更大的数字,如windows calc,如果我从键盘引入数字,它可以正常工作,但我不知道如何从button发送值 这是密码 <html> <head> <title>Calculadora Basica</title> </head> <body> <

我用PHP制作了一个基本的计算器发送给我的技术人员我有基本的html和PHP代码在同一个文件中,但我找不到方法将任何按钮的数字发送到文本以生成更大的数字,如windows calc,如果我从键盘引入数字,它可以正常工作,但我不知道如何从button发送值 这是密码

<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>

巴西加尔各拉多拉酒店

你必须更新你的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">



在head标记中调用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>

函数存储编号(obj)
{       
var previousVal=document.getElementById(“num1”).value;
previousVal=previousVal+对象值;
document.getElementById(“num1”).value=previousVal;
document.getElementById(“num2”).value=previousVal;
}  

谢谢。

text要生成一个更大的数字,意思是?如果我按下按钮1,3,5,0,num1必须得到与NUM2相同的1350值。这不是很难做到,除了一件事-表单如何知道将数字放入哪个输入框?你有两个独立的输入,当用户点击一个数字按钮时,就无法知道他们要把数字输入哪个框。我们不能使用当前焦点,因为一旦用户单击按钮,焦点就会移动到按钮本身。。。如果您将其修改为使用单个输入框,如真正的计算器,则实现起来会容易得多。tks mohini,我确实喜欢它,但要对我的文本框进行更大的更改,请为操作员和更多脚本再添加一个