-1

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.

Bruna Piloto
  • 19
  • 1
  • 8

1 Answers1

-1

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.

  • -1 vote, I can only assume that's because I didn't provide the answer the question was asking for? Explanation via comment would be appreciated. It could help myself or someone else better assist. – SyrslyTwitch May 05 '20 at 14:14