简体   繁体   English

我们将如何使用一个输出文本框在php中创建一个简单的计算器

[英]How we will make a simple calculator just in php with one output textbox

Here is my form and i want to make a simple calculator in php using with one text box 这是我的表格,我想用一个文本框在php中制作一个简单的计算器

<form action="calculator.php" method="POST">

  <input type="text" name="result">
  <br>
  <input type="button" name="one" value="1">
  <input type="button" name="two" value="2">
  <input type="button" name="three" value="3">
  <input type="button" name="four" value="4">
  <input type="button" name="five" value="5">
  <input type="button" name="six" value="6">
  <input type="button" name="seven" value="7">

  <input type="button" name="eight" value="8">
  <input type="button" name="nine" value="9">
  <input type="button" name="0" value="0">
  <input type="button" name="+" value="+">
  <input type="button" name="-" value="-">
  <input type="button" name="*" value="*">


</form>

If you send your request to calculator.php like this, you can't tell the difference between the numbers you pressed first. 如果您将此请求发送到这样的calculator.php,则无法区分您先按下的数字。

So first of all you need to put those numbers into a text field - maybe with javascript? 首先,您需要将这些数字放入文本字​​段 - 也许使用javascript? - and then you can transmit the form to calculator.php and do the math. - 然后你可以将表格发送到calculator.php并进行数学运算。 If you only got one textfield, you can simply split the string you will get and search for your operations. 如果您只有一个文本字段,则可以简单地拆分您将获得的字符串并搜索您的操作。

need to use some javascript, get the values and append to the text box 需要使用一些javascript,获取值并附加到文本框中

<script type="text/javascript" src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    $("#frmCalc input:button").on("click", function() {
        var par = $(this).val();            
        $("#result").val($("#result").val() + par);
    });
});
</script>
<form action="calculator.php" id="frmCalc" method="POST">
<input type="text" name="result" id="result">
<br>
<input type="button" name="one" value="1">
<input type="button" name="two" value="2">
<input type="button" name="three" value="3">
<input type="button" name="four" value="4">
<input type="button" name="five" value="5">
<input type="button" name="six" value="6">
<input type="button" name="seven" value="7">

<input type="button" name="eight" value="8">
<input type="button" name="nine" value="9">
<input type="button" name="0" value="0">
<input type="button" name="+" value="+">
<input type="button" name="-" value="-">
<input type="button" name="*" value="*">
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM