lundi 26 mars 2018

PHP Calc number of checkboxes ticked & Add their Values + Tax

I need to write a PHP script that computes the total cost of the ordered light bulbs and battery packs after adding 17.5 percent VAT. The program must inform the buyer of exactly what was ordered in a table, and what credit card was chosen to make payment.

Here is my code so far (it is a form with checkboxes and radio buttons to order light bulbs

bulbs.html

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<script>
function formSubmit() {
    document.forms["myForm"].submit();
}
</script>
</head>
<body>

<form name="myForm" action="calc.php" method="post">
Name: <input type="text" name="fullname"><br>

  <input type="checkbox" name="bulb[]" value="Bulb1"> Four 100-watt light bulbs for $2.39<br>
  <input type="checkbox" name="bulb[]" value="Bulb2"> Eight 100-watt light bulbs for $4.29<br>
  <input type="checkbox" name="bulb[]" value="Bulb3"> Four 100-watt long-life light bulbs for $3.95<br>
  <input type="checkbox" name="bulb[]" value="Bulb4"> Eight 100-watt long-life light bulbs for $7.49<br>


Number of battery packs you would like for $10.42 each: <input type="text" name="batterypacks"><br>

  <input type="radio" name="card" value="Card1"> Visa<br>
  <input type="radio" name="card" value="Card2"> Mastercard<br>
  <input type="radio" name="card" value="Card3"> American Express<br>

  <input type="submit" value="Submit">
</form>


</body>
</html>

this is my calc.php

<?php

echo $_POST;

echo "<br>";

echo $_POST['bulb'];




if(isset($_POST['bulb'])){
  if (is_array($_POST['bulb'])) {
    foreach($_POST['bulb'] as $value){
        $bulb1=2.39;
        $bulb2=4.29;
        $bulb3=3.95;
        $bulb4=7.49;
        $totalCost;
      echo $value;
    }
  } else {
    $value = $_POST['bulb'];
    echo $value;
  }
}



$price = 0;

foreach( $_POST['bulb'] as $key => $value ) {
   switch( $value ) {
      case 'Bulb1':
        $price = $price + 2.39;
        break;
      case 'Bulb2':
        $price = $price + 4.29;
        break;
      case 'Bulb3':
        $price = $price + 3.95;
        break;  
      case 'Bulb4':
        $price = $price + 7.49;
        break;
      
}
echo $price; // Total price

$afterTax = 0;

$price * .175 = $afterTax;

echo $afterTax;



$name = $_GET['card'];


foreach ($name as $card){ 
    echo $card."<br />";
}



?>

// if bulb 1 is selcted, add to total price
//compare checkedItem==Bulb1

else
checkedItem==Bulb2

else
checkedItem==Bulb3

I feel like I'm really close! However, whenever I click "submit" on bulbs.html, it just leads me to the code for calc.php and doesn't show any values at all. What am I missing in terms of calculating the checked boxes and adding tax?




Aucun commentaire:

Enregistrer un commentaire