Skip to content
Advertisement

PHP total order

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>

Advertisement

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement