Jump to content

Recommended Posts

Hi All, having trouble with the following and would be grateful for any assistance. The code below is the part of a multi-step form that calculates the sales tax of a simple order form for Authorize.net (AIM API). I would like to add an if, else condition that processes the $amount function with tax if the state is florida, otherwise, it processes the $amount without tax. Here is the code:

<?php

require_once '../config.php';


$coffee_store_relay_url = $site_root . "process_sale.php";

/**
 * Sets the possible coffee choices.
 */
$prices_array = array(
  "1 Case of Howards Best Coconut Water" => "24.99",
  "2 Cases of Howards Best Coconut Water" => "47.99",
);
$size = "small"; // Set Default Size
if (isset($_POST['size']) && isset($prices_array[$_POST['size']])) {
  $size = $_POST['size'];
}
if($state == 'FL')
$price = $prices_array[$size]; // Set Price
$tax = number_format($price * .07,2); // Set tax
$amount = number_format($price + $tax,2); // Set total amount
else
$price = $prices_array[$size]; // Set Price
$tax = number_format($price * .0,2); // Set tax
$amount = number_format($price + $tax,2); // Set total amount

Link to comment
https://forums.phpfreaks.com/topic/243403-if-else-condition-within-a-form/
Share on other sites

You have left off the curly braces

 

if($state == 'FL') {
    $price = $prices_array[$size]; // Set Price
    $tax = number_format($price * .07,2); // Set tax
    $amount = number_format($price + $tax,2); // Set total amount
} else {
    $price = $prices_array[$size]; // Set Price
    $tax = number_format($price * .0,2); // Set tax
    $amount = number_format($price + $tax,2); // Set total amount
}

Yes, thank you, but that was only part of my problem. The form is not processing the if, else properly. Here is the code to the first step (Registration) where the state value is captured.

<!doctype html>
<html>
  <head>
    <title>Your Store</title>
    <link rel="stylesheet" href="style.css">
    <script type="text/javascript" src="jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="jquery.validate.min.js"></script>
    <script type="text/javascript" src="jquery.validate.creditcardtypes.js"></script>
    <script>
      $(document).ready(function(){
        $("#register").validate();
      });
      </script>
  </head>
  <body><div class="container">
    <h2>Please Register</h2>
    <form method="post" action="select_size.php" id="register">
     <fieldset>        
        <div>
            <label>First Name</label>
            <input type="text" class="text required" size="15" name="x_first_name" >
          </input>
        </div>
        <div>
          <label>Last Name</label>
          <input type="text" class="text required" size="14" name="x_last_name" ></input>
        </div>
      </fieldset>
      <fieldset>
          <div>
          <label>Daytime Telephone or Cell #</label>
          <input type="text" class="text required" size="25" name="x_phone" ></input>
        </div>
      </fieldset>
            <fieldset>
          <div>
          <label>Ship To State</label>
          <input type="text" class="text required" size="2" name="x_state" ></input>
        </div>
      </fieldset>
      <input type="submit" class="submit" value="Continue">
    </form>
    <img src="footer_image.png" width="800" height="283"><img src="footer_bar.png" width="800" height="22"></div>
  </body>
</html>

 

 

I then post that value and use it as the condition in the next step:

<?php

require_once '../config.php';


$coffee_store_relay_url = $site_root . "process_sale.php";

/**
* Sets the possible coffee choices.
*/
$prices_array = array(
  "1 Case of Howards Best Coconut Water" => "24.99",
  "2 Cases of Howards Best Coconut Water" => "47.99",
);
$size = "small"; // Set Default Size
if (isset($_POST['size']) && isset($prices_array[$_POST['size']])) {
  $size = $_POST['size'];
}
$state = $_POST['x_state'];
if($state == 'FL')
$price = $prices_array[$size]; // Set Price
$tax = number_format($price * .07,2); // Set tax
$amount = number_format($price + $tax,2); // Set total amount
else
$price = $prices_array[$size]; // Set Price
$tax = number_format($price * .0,2); // Set tax
$amount = number_format($price + $tax,2); // Set total amount

 

As it is now, no matter what value is entered for state, it still multiplies it by the .07, rather than the .00.

 

Please help!!

 

You need curly-braces to group the code in the IF part and the ELSE part:

 

if($state == 'FL') {
$price = $prices_array[$size]; // Set Price
$tax = number_format($price * .07,2); // Set tax
$amount = number_format($price + $tax,2); // Set total amount
} else {
$price = $prices_array[$size]; // Set Price
$tax = number_format($price * .0,2); // Set tax
$amount = number_format($price + $tax,2); // Set total amount;
}

 

As you have it, you should get an error about the "unexpected ELSE". You should be developing code with error reporting on so you can see the errors. Put this at the top of your php page:

 

error_reporting(E_ALL);
ini_set('display_errors', 1);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.