简体   繁体   中英

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:

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

What am I doing wrong because it is not calculating? Thanks in advance!

Ps. Im using POST method on my form.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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