php PHP乘以两个字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18241039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:14:01  来源:igfitidea点击:

PHP multiply Two fields

php

提问by Maggie Ackermann

I am trying to multiply to fields together to obtain a total in PHP form.

我正在尝试将字段相乘以获得 PHP 形式的总数。

   <label for="190_mnth2"></label>
          <div align="center">
            <input name="190_mnth" type="text" id="190_mnth2" value="10" size="5" />
          </div></td>
        <td><div align="center">
          <label for="190_rate"></label>
          <input name="190_rate" type="text" id="190_rate" value="190.00" size="10" />
        </div></td>
        <td><div align="center">
          <input name="total_190" type="text" id="total_190" value=<? echo '190_mnth2' * '190_rate' ?> size="10" />

The above is my current code but the answer is totally wrong it gives me 36100 What is wrong with my formula if anyone can assist?

以上是我当前的代码,但答案完全错误,它给了我 36100 如果有人可以提供帮助,我的公式有什么问题?

回答by Mr. Alien

First of all you cannot calculate the total like that, it's not Javascript, you need a form with a get/post request which will send a request to the server, server will process and throw the calculated value back to the user.. so wrap the fields around forms, set your method to post(preferred) and than you can write your PHP code like

首先,你不能像这样计算总数,它不是 Javascript,你需要一个带有 get/post 请求的表单,它将向服务器发送一个请求,服务器将处理并将计算出的值返回给用户..所以换行表单周围的字段,将您的方法设置为 post(preferred),然后您可以编写您的 PHP 代码,例如

<?php
   if(isset($_POST['submit_button_name'])) { //Use $_GET if it's a GET request
       //Save the values in variable
       $mnth_190 = $_POST['190_mnth']; 
       $rate_190 = $_POST['190_rate'];

       //Calculate here
       $total = $mnth_190 * $rate_190;

       /* Now you can use $total either to echo straight in your page, 
      or inside another input field */
   }
?>

Also make sure you validate the data before the form is posted and is calculated, check whether the user input doesn't have string or any other special character.

还要确保在发布和计算表单之前验证数据,检查用户输入是否没有字符串或任何其他特殊字符。

回答by Erik Godard

The purpose of PHP is to generate HTML to display, not to update the HTML of the current page. You can create a POST request that submits your data for display on another page. If you want to dynamically update the total on the current page, you should use Javascript or another front end language.

PHP 的目的是生成 HTML 来显示,而不是更新当前页面的 HTML。您可以创建一个 POST 请求,提交您的数据以在另一个页面上显示。如果要动态更新当前页面的总数,则应使用 Javascript 或其他前端语言。

回答by Funk Forty Niner

I'm going to take a shot at doing something like this, as an example.

作为一个例子,我将尝试做这样的事情。

0_mnth2 = 10;
0_rate = 190;

$total = 0_mnth2 * 0_rate;

then using: value=<? echo '$total'; ?>

然后使用: value=<? echo '$total'; ?>

回答by fantasitcalbeastly

<? echo '190_mnth2' * '190_rate' ?>

You're attempting to multiply two strings, which will probably be converted by PHP as 190 * 190.

您正在尝试将两个字符串相乘,PHP 可能会将其转换为 190 * 190。

In order to get this to work, you're going to have to do it in two separate steps (with PHP anyway). Because PHP is a server side language, you'll have to $_POST[''], or submit these two values as part of the query string and use $_GET[''] to calculate.

为了让它起作用,您将不得不分两个单独的步骤来完成(无论如何都要使用 PHP)。因为 PHP 是一种服务器端语言,所以您必须 $_POST[''],或者将这两个值作为查询字符串的一部分提交并使用 $_GET[''] 进行计算。

If you don't want to do it this way, then I'd suggest looking at some JavaScript to handle it instead.

如果您不想这样做,那么我建议您查看一些 JavaScript 来处理它。