Categories: php, subtotal, webstore

PHP total order

2 answers

I have a site that displays items with radio buttons. I want to make the PHP code display the total for the user when they submit the code.

Below is the code I have so far along with the prices for each item that would be in the store.

<?php   $keybored = $_POST["keybored"]; $mouse = $_POST["mouse"]; $harddrive = $_POST["harddrive"]; $desktop = $_POST["desktop"];      $keyboredprice = 5.00; $mouseprice =  3.50; $hardriveprice = 20.00; $desktopprice = 100;    $total = $prices;  echo "$total";    ?> 

The HTML code I'm working with is posted below that is for the webstore.

<!DOCTYPE html>  <html>       <head>          <style>          body {background-color: lightblue;}          h1   {color: gray;}              h2 {color: white;                        border-size: 1;              width: 200px;    border: 15px solid black;    padding: 50px;    margin: 10px;}              p  {color: white;              font-size: 200%;}              img {width:350px;height:300px;}                        </style>      </head>  <body>      <form action="purchase.php" method="post">  <h1> Welcome to computer parts store</h1>  <h2>Keybored</h1>  <img src="keybored.jpg">  <p> Select how many <br>  <input type="radio" name="keybored" value="1"">1<br>  <input type="radio" name="keybored" value="2">2<br>  <input type="radio" name="keybored" value="3"> 3<br> <br>  </p>    <h2>Mouse</h1>  <img src="mouse.jpg">  <p> Select how many <br>  <input type="radio" name="mouse" value="1"">1<br>  <input type="radio" name="mouse" value="2">2<br>  <input type="radio" name="mouse" value="3"> 3<br> <br>  </p>    <h2>HarDrive</h1>  <img src="harddrive.jpeg" >  <p> Select how many <br>  <input type="radio" name="harddrive" value="1">1<br>  <input type="radio" name="harddrive" value="2">2<br>  <input type="radio" name="harddrive" value="3"> 3<br> <br>  </p>    <h2>Desktop</h1>  <img src="Desktop.jpg">  <p> Select how many <br>  <input type="radio" name="desktop" value="1">1<br>  <input type="radio" name="desktop" value="2">2<br>  <input type="radio" name="desktop" value="3"> 3<br> <br>  </p>      <p>     First Name: <input type="text" name="fname" ><br>         Last Name: <input type="text" name="lname" ><br>          Card type: <input type="text" name="type" > <br>         Card Number: <input type="text" name="Card" > <br>         Expiration date: <input type="text" name="exp" > <br>          <input type="submit" value="Submit order"><br>  </p>  </form>  </body>  </html>

All answers to this question, which has the identifier 61239019

The best answer:

Simple: quantity * price.

<?php   $keybored = $_POST["keybored"] ?? 0; $mouse = $_POST["mouse"] ?? 0; $harddrive = $_POST["harddrive"] ?? 0; $desktop = $_POST["desktop"] ?? 0;  $keyboredprice = 5.00; $mouseprice =  3.50; $hardriveprice = 20.00; $desktopprice = 100;  $total = ($keybored * $keyboredprice) + ($mouse * $mouseprice) + ($hardrive * $hardriveprice) + ($desktop * $desktopprice);  echo "$total"; 

PHP is loosely typed and will convert the form quantity strings to integers for you, and will also retain the floating point prices in the multiplication.

This uses the php7 null coalescing operator (??) to set the default quantity to 0 if any of the form elements are not submitted.

You need to calculate the price before you try to show its value to the user.
$price = $keyboredprice + $mouseprice + $hardriveprice + $desktopprice; $total = $price echo "$total"

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data