Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem with PHP. Same value always showing up

    Hello. I am currently learning PHP and have been working on this exercise. I want to to calculate the total order amount and add the provincial sales tax to it. For some reason almost every time I calculate the total amount it is always using the 13% sales tax. What am I doing wrong? Any help would be greatly appreciated. This is my code:

    HTML:
    Code:
    <html>
    <head>
    
    <script language="javascript" type="text/javascript">
    
    	function checkForm()
    	{
    		var errMessage = "";
    		var myForm = document.orderForm;
    
    
    		if (myForm.firstName.value == "")
    		{
    			errMessage += "\nPlease enter your first name";
    		}
    		if (myForm.lastName.value == "")
    		{
    			errMessage += "\nPlease enter your last name";
    		}
    		if (myForm.address.value == "")
    		{
    			errMessage += "\nPlease enter a valid address";
    		}
    		if (myForm.city.value == "")
    		{
    			errMessage += "\nPlease enter a valid city";
    		}
    		if (myForm.province.value == "one")
    		{
    			errMessage += "\nPlease select a province";
    		}
    		if (myForm.postalCode.value == "")
    		{
    			errMessage += "\nPlease enter a valid postal code";
    		}
    		if (myForm.purchaseItem.value == "")
    		{
    			errMessage += "\nPlease select an item to purchase";
    		}
    		if (myForm.quantity.value == "")
    		{
    			errMessage += "\nPlease enter a quantity amount";
    		}
    		if (isNaN(myForm.quantity.value))
    		{
    			errMessage += "\nPlease enter a valid quantity amount";
    		}
    
    
    		if (errMessage != "")
    		{
    			alert (errMessage);
    			return false;
    		}
    		return true;
    	}
    
    </script>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <form id="orderForm" name="orderForm" method="post" onSubmit="return checkForm()" action="exercise.php" >
      <table width="619" height="169" border="0">
        <tr>
          <td width="94" height="26">First Name:</td>
          <td width="205"><label>
            <input name="firstName" type="text" id="firstName" />
          </label></td>
          <td width="168">Item To Be Purchased:</td>
          <td width="124"><label>
            <select name="purchaseItem" id="purchaseItem">
            <option value="">Select Item</option>
              <option value="video">Video Card</option>
              <option value="case">Computer Case</option>
              <option value="cpuFan">CPU Fan</option>
              <option value="caseFan">Case Fan</option>
              <option value="keyboard">Keyboard</option>
              <option value="mouse">Mouse</option>
            </select>
          </label></td>
        </tr>
        <tr>
          <td height="26">Last Name:</td>
          <td><label>
            <input type="text" name="lastName" id="lastName" />
          </label></td>
          <td>Quantity:</td>
          <td><label>
            <input name="quantity" type="text" id="quantity" maxlength="3" />
          </label></td>
        </tr>
        <tr>
          <td height="26">Address:</td>
          <td><input type="text" name="address" id="address" /></td>
          <td>Extra:</td>
          <td><label>
            <input type="checkbox" name="ram" id="ram" />
          RAM<br />
          <input type="checkbox" name="cpu" id="cpu" />
          CPU      <br />
          </label></td>
        </tr>
        <tr>
          <td height="26">City:</td>
          <td><label>
            <input type="text" name="city" id="city" />
          </label></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td height="26">Province:</td>
          <td><label>
            <select name="province" id="province">
            <option value="one">Please Select a Province</option>
              <option value="AB">Alberta</option>
              <option value="BC">British Columbia</option>
              <option value="MB">Manitoba</option>
              <option value="NB">New Brunswick</option>
              <option value="NL">Newfoundland and Labrador</option>
              <option value="NWT">Northwest Territories</option>
              <option value="NS">Nova Scotia</option>
              <option value="NU">Nunavut</option>
              <option value="ON">Ontario</option>
              <option value="PEI">Prince Edward Island</option>
              <option value="QC">Quebec</option>
              <option value="SK">Saskatchewan</option>
              <option value="YT">Yukon</option>
            </select>
          </label></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td height="23">Postal Code:</td>
          <td><label>
            <input type="text" name="postalCode" id="postalCode" />
          </label></td>
          <td><label>
            <div align="center">
              <input type="reset" name="reset" id="reset" value="Reset" />
            </div>
          </label></td>
          <td><label>
            <input type="submit" name="submit" id="submit" value="submit"/>
          </label></td>
        </tr>
      </table>
    </form>
    </body>
    </html>
    PHP:
    Code:
    <?php
    
    	$firstName = $_POST["firstName"];
    	$lastName = $_POST["lastName"];
    	$address = $_POST["address"];
    	$city = $_POST["city"];
    	$province = $_POST["province"];
    	$postalCode = $_POST["postalCode"];
    	$item = $_POST["purchaseItem"];
    	$quantity = $_POST["quantity"];
    	$operatingSystem = $_POST["operatingSystem"];
    	$itemCost;
    	$salesTax;
    	$shippingCharge;
    	$delivery;
    
    
    	print ("<br> <br>");
    	print ("Shipping To: $lastName, $firstName <br>");
    	print ("$address <br>");
    	print ("$city, $province <br>");
    	print ("$postalCode <br>");
    	print ("<br> <br>");
    
    
    	if ($item == "video")
    	{
    		$itemCost = 350;
    	}
    	elseif ($item == "case")
    	{
    		$itemCost = 55;
    	}
    	elseif ($item == "cpuFan")
    	{
    		$itemCost = 18;
    	}
    	elseif ($item == "caseFan")
    	{
    		$itemCost = 12;
    	}
    	elseif ($item == "keyboard")
    	{
    		$itemCost = 28;
    	}
    	else
    	{
    		$itemCost = 14;
    	}
    
    
    
    	if ($province == "AB")
    	{
    		$salesTax = 0.05;
    	}
    	elseif ($province == "BC")
    	{
    		$salesTax = 0.12;
    	}
    	elseif ($province == "MB")
    	{
    		$salesTax = 0.12;
    	}
    	elseif ($province == "NB")
    	{
    		$salesTax = 0.13;
    	}
    	elseif ($province == "NL")
    	{
    		$salesTax = 0.13;
    	}
    	elseif ($province == "NS")
    	{
    		$salesTax = 0.13;
    	}
    	elseif ($province = "ON")
    	{
    		$salesTax = 0.13;
    	}
    	elseif ($province = "PEI")
    	{
    		$salesTax = 0.155;
    	}
    	elseif ($province = "QC")
    	{
    		$salesTax = 0.12875;
    	}
    	elseif ($province = "SK")
    	{
    		$salesTax = 0.10;
    	}
    	else
    	{
    		$salesTax = 0;
    	}
    
    
    
    	$totalItemCost = ($itemCost * $quantity);
    	$totalTaxItem = ($totalItemCost * $salesTax) + $totalItemCost;
    
    	if ($totalTaxItem <= 25)
    	{
    		$shippingCharge = 3;
    		$delivery = 1;
    	}
    	elseif ($totalTaxItem > 25 && $totalTaxItem <= 50)
    	{
    		$shippingCharge = 4;
    		$delivery = 1;
    	}
    	elseif ($totalTaxItem > 50 && $totalTaxItem <= 75)
    	{
    		$shippingCharge = 5;
    		$delivery = 3;
    	}
    	else
    	{
    		$shippingCharge = 6;
    		$delivery = 4;
    	}
    
    
    	$totalOrderCost = $totalTaxItem + $shippingCharge;
            $totalSaleTax = $salesTax * 100;
    
    	print ("Order Information: <br>");
    	print ("Item Cost: $$itemCost <br>");
    	print ("Quantity: $quantity <br>");
    	print ("Tax: $totalSaleTax %<br>");
    	print ("Delivery Charge: $$shippingCharge <br>");
    	print ("Total Order Is: $$totalOrderCost <br>");
    	print ("Shipping: $delivery buisness day(s) <br>");
    
    ?>
    Last edited by system32; 03-13-2010 at 09:14 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Your "if" statements must use == not =

    elseif ($province = "ON")
    {
    $salesTax = 0.13;
    }
    elseif ($province = "PEI")
    {
    $salesTax = 0.155;
    }
    elseif ($province = "QC")
    {
    $salesTax = 0.12875;
    }
    elseif ($province = "SK")
    {
    $salesTax = 0.10;
    }

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you. Appreciate it


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •