简体   繁体   English

回显 Php 中的输入值

[英]Echo the input value in Php

Im pretty new into PHP and Im trying to calculate the quantity written inside the input (quantity) My code looks like this:我对 PHP 很陌生,我试图计算输入中写入的数量(数量)我的代码如下所示:

<tr>
    <td>Antal:</td>
    <td><input type="number" name="quantity" value=" "></td>       
</tr>
<tr>
    <td>Styck à pris 110 kr</td>
    <td><input type=submit></td>
</tr>

and the php:和 php:

<?php
if(isset($_POST['quantity']) && $_POST['quantity'] == 1){
    $antalBiljetter = $_POST['quantity'];
    $isSubmit = 1; 

    $total = $antalBiljetter*110;
    echo "<br>Totalpris för" .$antalBiljetter['quantity']. " är " .$total;
}else{
    $isSubmit = 0; 
    echo "<br>Gå tillbaka och välj antal biljetter.";
}
?>

So what i would like to output is quantity * 110.所以我想要 output 是数量 * 110。

What am I doing wrong because it is not calculating?我做错了什么,因为它不计算? Thanks in advance!提前致谢!

Ps.附言。 Im using POST method on my form.我在我的表单上使用 POST 方法。

Assuming you want the user to click submit first and then the submitted data gets processed by that script and displayed as the submitted "quantity" value multiplied by 110, change your code so it no longer requires the quantity to be exactly 1:假设您希望用户先单击提交,然后提交的数据由该脚本处理并显示为提交的“数量”值乘以 110,请更改您的代码,使其不再要求数量正好为 1:

<?php
if(isset($_POST['quantity']) && $_POST['quantity'] >= 1){
?>

Sanitizing your post values is wise as well.清理您的帖子值也是明智的。 You should probably check if the value is numeric (use the php is_numeric function) and have some other rules there, but this at least solves your immediate problem.您可能应该检查该值是否为数字(使用 php is_numeric 函数)并在那里有一些其他规则,但这至少可以解决您的直接问题。

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

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