samedi 24 mars 2018

If statement for buying lightbulbs (update)

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

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

In my calc.php file, I have included this:

<?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;
  }
}






?>

My last step is to create IF statements that would run through the checked boxes based on what the user selected, and calculate the total price of the bill. It would look something like this:

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

else checkedItem==Bulb2

else checkedItem==Bulb3




Aucun commentaire:

Enregistrer un commentaire