简体   繁体   English

使用PHP中的用户输入的公式计算值

[英]Calculating values with formulae from user input in PHP

I want to do an equation using the values I get from the input boxes called ampMin, voltMin and hastMin... 我想使用从输入框中获得的值ampMin,voltMin和hastMin来做一个方程式...

I'm not sure if it's a syntax problem or my method of approach is plain wrong.. Here's is an example of how the equation should look and work like with Excel. 我不确定这是语法问题还是我的方法很错误。.这是一个公式的示例,该示例如何与Excel一起使用。

What am I doing wrong? 我究竟做错了什么? Thank you for your time! 感谢您的时间!

EDIT: worth mentioning is that the whole block of code is within "strckenergi.php". 编辑:值得一提的是,整个代码块都在“ strckenergi.php”内。

Qmax的Excel方程

<html>
<title>Sträckenergi</title>
<body>
    <h3>Svetsmetod: 111</h3>
    <h4><i>Med K=0.8</i></h4>
    <pre>
        <form method="post" action="strckenergi.php">
            Amp. Min <input type="text" name="ampMin">  Volt. Min <input type ="text" name="voltMin"> Hast. Min <input type="text" name="hastMin"> </pre>
        <?php
        echo "kJ/mm (minimum) = " . $qMin
        $qMin = ( $ampMin * $voltMin ) / (( $hastMin * 1000 ) / $hastMin * 0.8));
        ?>  
</body>
</html>

You don't appear to be using forms correctly. 您似乎没有正确使用表单。 If you want the browser do be able to display it right away, you should use JavaScript instead. 如果希望浏览器能够立即显示它,则应改用JavaScript。 PHP will require an additional pageload, or an AJAX request. PHP将需要额外的页面加载或AJAX请求。

And in order to post the form, you need a submit button. 为了发布表格,您需要一个提交按钮。 Otherwise, the browser won't know what to do with it. 否则,浏览器将不知道如何处理。

Further, your first PHP line needs a semi-colon, and your second one needs to be above the first - otherwise, the interpreter won't know what your value is when printing it, because it hasn't been calculated yet. 此外,您的第一行PHP需要使用分号,而第二行必须位于第一行的上方-否则,解释器在打印时将不知道您的值,因为尚未计算出该值。

To be honest, I think you need to start by googling for how to construct an HTML form, then you can look up simple JavaScripts. 老实说,我认为您需要首先搜索如何构造HTML表单,然后才能查找简单的JavaScript。 Lycka till! Lycka直到!

First off, as Joel Hinz says above, you need a submit button so the page knows when the user is done inputting and wants to send the form to the server for processing. 首先,如Joel Hinz所说,您需要一个提交按钮,以便页面知道用户何时输入完毕,并希望将表单发送到服务器进行处理。

Second, you need to close the form with a tag. 其次,您需要使用标签关闭表单。

Third, you're probably better off sticking with php at this stage; 第三,现阶段最好还是坚持使用php。 JavaScript can be a bit tricky and mighty frustrating for beginners. 对于初学者来说,JavaScript可能会有些棘手和令人沮丧。 This is a rough approximation of how your form should look. 这是您的表单外观的粗略近似。

<form method="post" action="strckenergi.php">
        Amp. Min  <input type="text" name="ampMin"> 
        Volt. Min <input type ="text" name="voltMin">
        Hast. Min <input type="text" name="hastMin">

       <input type="submit" value="submit">
</form>        

See http://www.tizag.com/phpT/forms.php for a clear explanation of forms. 有关表单的清晰说明,请参见http://www.tizag.com/phpT/forms.php

OK first thing first. 好,先做第一件事。 When the form is submitted, the values contained in the $_POST array aren't immediately available to the script on the server which processes the input. 提交表单后,$ _ POST数组中包含的值将无法立即用于处理输入的服务器上的脚本。

For that you will need something like the following: 为此,您将需要以下内容:

<?php
     if($_POST){// this checks for the existence of the $_POST array, i.e. was something submitted    
               //now we're assuming a form was submitted
             $voltMin = $_POST['voltMin'];
             $ampMin  = $_POST['ampMin'];
             $hastMin = $_POST['hastMin'];


             $qMin = ( ($ampMin*$voltMin)/($hastMin*1000)/($hastMin*0.8) );
             echo "kJ/mm (minimum) = " . $qMin;
     }// if($_POST)...
   ?>  

Then you can process the elements, and print out the results of your calculations. 然后,您可以处理元素,并打印出计算结果。 Oh, and drop the pre tags unless you really need them. 哦,除非您确实需要它们,否则请放下pre标签。

This works. 这可行。

<html>
<title>Sträckenergi</title>
<body>
<h3>Svetsmetod: 111</h3>
<h4><i>Med K=0.8</i></h4>
     <pre>
    <form method="post" action="form.php">
Amp. Min <input type="text" name="ampMin">  Volt. Min <input type ="text"       name="voltMin"> Hast. Min <input type="text" name="hastMin">
    <input type="submit" value="submit"> </pre>
</form>

<?php
    if($_POST){

        $voltMin = $_POST['voltMin'];
        $ampMin = $_POST['ampMin'];
        $hastMin = $_POST['hastMin'];

        $qMin = ( $ampMin * $voltMin ) / ( $hastMin * 1000 ) / $hastMin * 0.8;
        echo "kJ/mm (minimum) = " . $qMin;
    }
    ?>

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

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