Jump to content

Total off by penny - can't figure out how


Go to solution Solved by mac_gyver,

Recommended Posts

I have an order site.  I wrote some of the code, but not all.  When someone orders from this page:

<?php
include 'header.php';
if(isset($_POST['redo'])){
	extract($_POST);

}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Journal Individual Order Form</title>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<link href="http://celt.edu/ject/images/favicon.png" rel="shortcut icon">
<style type="text/css">
</style>
<script type="text/javascript">
	function remove_space(ids,vals){
		document.getElementById(ids).value=vals.replace(/(^\s*)/g, "");
		var new_txt=document.getElementById(ids).value;
		document.getElementById(ids).value=new_txt.replace(/'/g, "");
}
function form_validate(){
	var name=document.getElementById('firstname').value;
	var lname=document.getElementById('lastname').value;
	var email=document.getElementById('email').value;
	var phone=document.getElementById('phone').value;
	var address=document.getElementById('address').value;
	var city=document.getElementById('city').value;
	var zip=document.getElementById('zip').value;
	 var regemail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,6})$/;
	var mess="";
	if(name==""){
		mess= mess+"Please Enter Your First Name \n";
	}
	if(lname==""){
		mess= mess+"Please Enter Your Last Name \n";
	}
	if(phone==""){
		mess= mess+"Phone cannot be Blank \n";
	} 
	if(email==""){
		mess= mess+"Email cannot be Blank \n";
	} 
	if(email!=""){
		if(! regemail.test(email)){
			mess= mess+"Invalid Email Format \n";
		}
			
	}	
	if(address==""){
		mess= mess+"Please Enter Your Street \n";
	}
	if(city==""){
		mess= mess+"Please Enter Your City \n";
	}
	if(zip==""){
		mess= mess+"Please Enter Your Zip \n";
	}
	if(document.subscriptionForm.payment_method[0].checked==false && (document.subscriptionForm.payment_method[1].checked==false && document.subscriptionForm.payment_method[2].checked==false)){
		mess= mess+"Please Select The Payment Type \n";
	}
	
	if(mess){
		alert(mess);
		return false;
	}
	return true;
}

</script>
</head>
<body onLoad="recalc()">
    <h1 style="margin-top: 0;">Individual subscription order form</h1>
    <h2><b><FONT COLOR="#C41230">Discounts are offered to subscribers who purchase subscriptions for two or more CELTUA journals.</FONT></b></h2>
    <h3>Choose two journals for <?php echo DISCOUNT_2Journals_per; ?>% off your total order or subscribe to all three for  <?php echo DISCOUNT_3Journals_per; ?>% off your total order. </h3>
    
    
    <p>Individual subscriptions can be done three ways: </p>
    <ul>
        <li>
            an individual print subscription, shipped to the address you provide
            <ul>
                <li>4 issues per year for <em>Journal on Excellence</em>, new subscriptions will start with Volume <?php echo $JECTsubscribeVolume['vol']; ?>, <?php echo $JECTsubscribeVolume['year']; ?></li>
                <li>1 issue per year for <em>Journal on Centers</em>, new subscriptions will start with Volume <?php echo $JCTLsubscribeVolume['vol']; ?>, <?php echo $JCTLsubscribeVolume['year']; ?></li>
                <li>1 issue per year for <em>Learning Communities</em>, new subscriptions will start with Volume <?php echo $LCJsubscribeVolume['vol']; ?>, <?php echo $LCJsubscribeVolume['year']; ?></li> 
            </ul>
        </li>
        <li>an individual electronic subscription (access to complete online archive) 
            -- electronic subscribers can browse articles by issue and search article abstracts</li>
        <li>a combination print and electronic subscription at a special discount rate</li>
    </ul>
    <p>Select the subscription option that suits your needs using the form below. 
       Print subscriptions can only be shipped to the US and Canada.</p>
    <form action='order_review.php' method='post' id='subscriptionForm' name="subscriptionForm" onsubmit="return form_validate()">
    <input type="hidden" name="subscription_type" value="individual"/>
    <input type="hidden" name="id_subcount" value="" id="id_subcount"/>
	 <input type="hidden" name="jectprice_hidden" id="jectprice_hidden" value=""/>
	 <input type="hidden" name="jctlprice_hidden" id="jctlprice_hidden" value=""/>
	 <input type="hidden" name="lcjprice_hidden" id="lcjprice_hidden" value=""/>
        <h2>Recipient's contact information</h2>
        <table>
        <tr>
            <th>First name:</th>
            <td><input type='text' name='firstname' id="firstname"  onblur="remove_space(this.id,this.value)" value="<?php if(isset($firstname)) { echo $firstname;} ?>" /></td>
        </tr>
        <tr>
            <th>Last name:</th>
            <td><input type='text' name='lastname' value="<?php if(isset($lastname)) { echo $lastname;} ?>"  onblur="remove_space(this.id,this.value)" id="lastname"/></td>
        </tr>
        <tr>
             <th>Department (if applicable):</th>
             <td><input type='text' name='department' value="<?php if(isset($department)) { echo $department;} ?>" onblur="remove_space(this.id,this.value)" id="department"/></td>
        </tr>
        <tr>
            <th>Institution (if applicable):</th>
            <td><input type='text' name='institution'  value="<?php if(isset($institution)) { echo $institution;} ?>" onblur="remove_space(this.id,this.value)" id="institution"/></td>
        </tr>
        <tr>
            <th>Phone number:</th>
            <td><input type='text' name='phone' value="<?php if(isset($phone)) { echo $phone;} ?>" onblur="remove_space(this.id,this.value)" id="phone"/></td>
        </tr>
        <tr>
            <th>Email address:</th>
            <td><input type='text' name='email' value="<?php if(isset($email)) { echo $email;} ?>" onblur="remove_space(this.id,this.value)" id="email"/></td>
        </tr>
        <tr>
            <th>Street address:</th>
            <td><textarea name='address' cols='40' rows='5' onblur="remove_space(this.id,this.value)" id="address"/><?php if(isset($address)) { echo $address;} ?></textarea></td>
        </tr>
        <tr>
            <th>City:</th>
            <td><input type='text' name='city' value="<?php if(isset($city)) { echo $city;} ?>" onblur="remove_space(this.id,this.value)" id="city"/></td>
        </tr>
        <tr>
            <th>State/Province:</th>
            <td><input type='text' name='state' value="<?php if(isset($state)) { echo $state;} ?>" onblur="remove_space(this.id,this.value)" id="state"/></td>
        </tr>
        <tr>
            <th>ZIP/Postal code:</th>
            <td><input type='text' name='zip' value="<?php if(isset($zip)) { echo $zip;} ?>" onblur="remove_space(this.id,this.value)" id="zip"/></td>
        </tr>
        <tr>
            <th>Country:</th>
            <td>
                <select name='usCan' id="usCan">
                    <option value='United States' >United States</option>
                    <option value='Canada' >Canada</option>
                    <option value='Other' >Other:</option>
                </select>
                <input type='text' name='country' id='country' value="<?php if(isset($usCan)) { echo $usCan;} ?>" >
            </td>
        </tr>
        </table>

        <h2>Subscription Options</h2>
        <h3>Please choose the options you desire.</h3>
        <FONT SIZE="+1">
        <h3><b><em>Journal on Excellence</em></b></h3>
        </FONT>
        <input type="radio" name="JECT" value="no" checked <?php if(@$JECT=="no"){ echo "checked";}?>>
        No thanks</br>
        <input type='radio' name='JECT' value='JECTindividual_print' <?php if(@$JECT=="JECTindividual_print"){ echo "checked";}?>>
        Print subscription (US and Canada only): US $<?php echo $JECTindividual_print_price['base']['United States']; ?>, Canada $<?php echo $JECTindividual_print_price['base']['Canada']; ?></br>
        <input type='radio' name='JECT' value='JECTindividual_electronic' <?php if(@$JECT=="JECTindividual_electronic"){ echo "checked";}?>>
        Electronic subscription (access to complete online archive): $<?php echo $JECTindividual_electronic_price['base']; ?></br>
        <input type='radio' name='JECT' value='JECTindividual_combo' <?php if(@$JECT=="JECTindividual_combo"){ echo "checked";}?>>
        Combination print and electronic subscription (US and Canada only): $<?php echo $JECTindividual_combo_price['base']; ?></br>
        <p style="text-indent:30px;">Length of subscription:</br></p>
            <label><input type="radio" name="JECTsub" value="1" checked <?php if(@$JECTsub=="1"){ echo "checked";}?>> 1 year</label></br>
            <label><input type="radio" name="JECTsub" value="2" <?php if(@$JECTsub=="2"){ echo "checked";}?>> 2 years</label></br>
            <label><input type="radio" name="JECTsub" value="3" <?php if(@$JECTsub=="3"){ echo "checked";}?>> 3 years</label></br>
            <label><input type='checkbox' name='JECTrenewal' value='yes' <?php if(@$JECTrenewal=="yes"){ echo "checked";}?>> Check box if this is a renewal</label></br>
    
    
        <FONT SIZE="+1">
        <h3><b><em>Journal on Centers</em></b></h3>
        </FONT>
        <input type="radio" name="JCTL" value="no" checked <?php if(@$JCTL=="no"){ echo "checked";}?>>
        No thanks</br>
        <input type='radio' name='JCTL' value='JCTLindividual_print' <?php if(@$JCTL=="JCTLindividual_print"){ echo "checked";}?>>
        Print subscription (US and Canada only): US $<?php echo $JCTLindividual_print_price['base']['United States']; ?>, Canada $<?php echo $JCTLindividual_print_price['base']['Canada']; ?></br>
        <input type='radio' name='JCTL' value='JCTLindividual_electronic' <?php if(@$JCTL=="JCTLindividual_electronic"){ echo "checked";}?>>
        Electronic subscription (access to complete online archive): $<?php echo $JCTLindividual_electronic_price['base']; ?></br>
        <input type='radio' name='JCTL' value='JCTLindividual_combo' <?php if(@$JCTL=="JCTLindividual_combo"){ echo "checked";}?>>
        Combination print and electronic subscription (US and Canada only): $<?php echo $JCTLindividual_combo_price['base']; ?></br>
        <p style="text-indent:30px;">Length of subscription:</br></p>
            <label><input type="radio" name="JCTLsub" value="1" checked <?php if(@$JCTLsub=="1"){ echo "checked";}?>> 1 year</label></br>
            <label><input type="radio" name="JCTLsub" value="2" <?php if(@$JCTLsub=="2"){ echo "checked";}?>> 2 years</label></br>
            <label><input type="radio" name="JCTLsub" value="3" <?php if(@$JCTLsub=="3"){ echo "checked";}?>> 3 years</label></br>
            <label><input type='checkbox' name='JCTLrenewal' value='yes' <?php if(@$JCTLrenewal=="yes"){ echo "checked";}?>> Check box if this is a renewal</label></br>

        <FONT SIZE="+1">
        <h3><b><em>Learning Communities</em></b></h3>
        </FONT>
        <input type="radio" name="LCJ" value="no" checked <?php if(@$LCJ=="no"){ echo "checked";}?>>
        No thanks</br>
        <input type='radio' name='LCJ' value='LCJindividual_print' <?php if(@$LCJ=="LCJindividual_print"){ echo "checked";}?>>
        Print subscription (US and Canada only): US $<?php echo $LCJindividual_print_price['base']['United States']; ?>, Canada $<?php echo $LCJindividual_print_price['base']['Canada']; ?></br>
        <input type='radio' name='LCJ' value='LCJindividual_electronic' <?php if(@$LCJ=="LCJindividual_electronic"){ echo "checked";}?>>
        Electronic subscription (access to complete online archive): $<?php echo $LCJindividual_electronic_price['base']; ?></br>
        <input type='radio' name='LCJ' value='LCJindividual_combo' <?php if(@$LCJ=="LCJindividual_combo"){ echo "checked";}?>>
        Combination print and electronic subscription (US and Canada only): $<?php echo $LCJindividual_combo_price['base']; ?></br>
        <p style="text-indent:30px;">Length of subscription:</br></p>
            <label><input type="radio" name="LCJsub" value="1" checked <?php if(@$LCJsub=="1"){ echo "checked";}?>> 1 year</label></br>
            <label><input type="radio" name="LCJsub" value="2" <?php if(@$LCJsub=="2"){ echo "checked";}?>> 2 years</label></br>
            <label><input type="radio" name="LCJsub" value="3" <?php if(@$LCJsub=="3"){ echo "checked";}?>> 3 years</label></br>
            <label><input type='checkbox' name='LCJrenewal' value='yes' <?php if(@$LCJrenewal=="yes"){ echo "checked";}?>> Check box if this is a renewal</label></br>
    
       
        
        <h2>Payment</h2>
        <h3 id="grandTotal">Total: $0.00</h3>
        <input type="hidden" name="total">
		<input type="hidden" name="subtotal">
		<input type="hidden" name="discount">
		<input type="hidden" name="itemprice1">
		<input type="hidden" name="itemprice2">
		<input type="hidden" name="itemprice3">


        <p>Please indicate how you wish to pay:</p>
        <table class="payment">
        <tr>
             <td>
                     <input type='radio' name='payment_method' value='Credit card (online)' <?php if(@$payment_method=="Credit card (online)"){ echo "checked";}?>> <b>Credit card (online)</b>
             </td>
             <td>
                 You will be taken to our secure payment site once you verified your order. 
                 Please have your credit card ready.
                 <strong>(MasterCard, Visa, Discover, or American Express)</strong>
             </td>
        </tr>
        <tr>
            <td>
                    <input type='radio' name='payment_method' value='Credit card (phone)' <?php if(@$payment_method=="Credit card (phone)"){ echo "checked";}?>> <b>Credit card (phone)</b>
            </td>
            <td>
                Please contact Matthew to make your payment. 
                <strong>(MasterCard or Visa only)</strong>
            </td>
        </tr>
        <tr>
            <td>
                    <input type='radio' name='payment_method' value='Check' <?php if(@$payment_method=="no"){ echo "Check";}?>> <b>Check or Money order</b></td>
            <td>Please make check or money order to:</br>
			Subscription Manager</td>
        </tr>
        </table>


<p>Once you have completed this form and agree to the above terms, click the "Continue" button below to review your order and view your invoice.  Your order will not be placed until you approve the invoice in the next step.</p>

        <p><input type="submit" name="submit" value="Continue"></p>
   </form>
</div> <!-- end of mainbody -->

<script type="text/javascript">  
 
var prices = {
    "United States" : 
        {
           "JECT" : [ 0, <?php echo $JECTindividual_print_price['base']['United States'] ?>, <?php echo $JECTindividual_electronic_price['base'] ?>, <?php echo $JECTindividual_combo_price['base'] ?> ],
           "JCTL" : [ 0, <?php echo $JCTLindividual_print_price['base']['United States'] ?>, <?php echo $JCTLindividual_electronic_price['base'] ?>, <?php echo $JCTLindividual_combo_price['base'] ?> ],
           "LCJ"  : [ 0, <?php echo $LCJindividual_print_price['base']['United States'] ?>, <?php echo $LCJindividual_electronic_price['base'] ?>, <?php echo $LCJindividual_combo_price['base'] ?> ],
		    "JECT_10" : [ 0, <?php echo $JECTindividual_print_price['10']['United States'] ?>, <?php echo $JECTindividual_electronic_price['10'] ?>, <?php echo $JECTindividual_combo_price['10'] ?> ],
			 "JCTL_10" : [ 0, <?php echo $JCTLindividual_print_price['10']['United States'] ?>, <?php echo $JCTLindividual_electronic_price['10'] ?>, <?php echo $JCTLindividual_combo_price['10'] ?> ],
			 "LCJ_10"  : [ 0, <?php echo $LCJindividual_print_price['10']['United States'] ?>, <?php echo $LCJindividual_electronic_price['10'] ?>, <?php echo $LCJindividual_combo_price['10'] ?> ],
			 "JECT_15" : [ 0, <?php echo $JECTindividual_print_price['15']['United States'] ?>, <?php echo $JECTindividual_electronic_price['15'] ?>, <?php echo $JECTindividual_combo_price['15'] ?> ],
			 "JCTL_15" : [ 0, <?php echo $JCTLindividual_print_price['15']['United States'] ?>, <?php echo $JCTLindividual_electronic_price['15'] ?>, <?php echo $JCTLindividual_combo_price['15'] ?> ],
			 "LCJ_15"  : [ 0, <?php echo $LCJindividual_print_price['15']['United States'] ?>, <?php echo $LCJindividual_electronic_price['15'] ?>, <?php echo $LCJindividual_combo_price['15'] ?> ]
        },
    "Canada" : 
        {
           "JECT" : [ 0, <?php echo $JECTindividual_print_price['base']['Canada'] ?>, <?php echo $JECTindividual_electronic_price['base'] ?>, <?php echo $JECTindividual_combo_price['base'] ?> ],
           "JCTL" : [ 0,<?php echo $JCTLindividual_print_price['base']['Canada'] ?>, <?php echo $JCTLindividual_electronic_price['base'] ?>, <?php echo $JCTLindividual_combo_price['base'] ?> ],
           "LCJ"  : [ 0, <?php echo $LCJindividual_print_price['base']['Canada'] ?>, <?php echo $LCJindividual_electronic_price['base'] ?>, <?php echo $LCJindividual_combo_price['base'] ?> ],
		   "JECT_10" : [ 0, <?php echo $JECTindividual_print_price['10']['Canada'] ?>, <?php echo $JECTindividual_electronic_price['10'] ?>, <?php echo $JECTindividual_combo_price['10'] ?> ],
           "JCTL_10" : [ 0,<?php echo $JCTLindividual_print_price['10']['Canada'] ?>, <?php echo $JCTLindividual_electronic_price['10'] ?>, <?php echo $JCTLindividual_combo_price['10'] ?> ],
           "LCJ_10"  : [ 0, <?php echo $LCJindividual_print_price['10']['Canada'] ?>, <?php echo $LCJindividual_electronic_price['10'] ?>, <?php echo $LCJindividual_combo_price['10'] ?> ],
		    "JECT_15" : [ 0, <?php echo $JECTindividual_print_price['15']['Canada'] ?>, <?php echo $JECTindividual_electronic_price['15'] ?>, <?php echo $JECTindividual_combo_price['15'] ?> ],
           "JCTL_15" : [ 0,<?php echo $JCTLindividual_print_price['15']['Canada'] ?>, <?php echo $JCTLindividual_electronic_price['15'] ?>, <?php echo $JCTLindividual_combo_price['15'] ?> ],
           "LCJ_15"  : [ 0, <?php echo $LCJindividual_print_price['15']['Canada'] ?>, <?php echo $LCJindividual_electronic_price['15'] ?>, <?php echo $LCJindividual_combo_price['15'] ?> ]
        },
    "Other" : 
        {
           "JECT" : [ 0, -1, <?php echo $JECTindividual_electronic_price['base'] ?>, -1 ],
           "JCTL" : [ 0, -1, <?php echo $JCTLindividual_electronic_price['base'] ?>, -1 ],
           "LCJ"  : [ 0, -1, <?php echo $LCJindividual_electronic_price['base'] ?>, -1 ]
        }
		
    };
	

var form = document.getElementById("subscriptionForm");

// set up the calls to recalc from all checkboxes and radio buttons
var inps = form.getElementsByTagName("input");
for ( var i = 0; i < inps.length; ++i )
{
    var inp = inps[i];
    if ( inp.type == "checkbox" || inp.type == "radio" )
    {
        inp.onclick = recalc;
    }
}
// and also from a change of country:
form.usCan.onchange = recalc;


function groupValue( group )
{
    for ( var g = 0; g < group.length; ++g )
    {
        if ( group[g].checked ) return g;
    }
    return 0;
}


function recalc( )
{
    var msg = document.getElementById("grandTotal");
    var country = form.usCan.value;
    var priceTable = prices[country];

    var ject = groupValue( form.JECT );
    var jctl = groupValue( form.JCTL );
    var lcj  = groupValue( form.LCJ  );
	var jectprice_hidden=0;
	var jctlprice_hidden=0;
	var lcjprice_hidden=0;
	 var subcount = ( ject > 0  ? 1 : 0 ) + ( jctl > 0 ? 1 : 0 ) + ( lcj > 0 ? 1 : 0 );
	var jectprice = priceTable["JECT"][ ject ];
    var jctlprice = priceTable["JCTL"][ jctl ];
    var lcjprice  = priceTable["LCJ" ][ lcj  ];
	if(subcount==1){
		var jectprice_hidden = priceTable["JECT"][ ject ];
		var jctlprice_hidden = priceTable["JCTL"][ jctl ];
		var lcjprice_hidden  = priceTable["LCJ" ][ lcj  ];
	}
	if(subcount==2){
		var jectprice_hidden = priceTable["JECT_10"][ ject ];
		var jctlprice_hidden = priceTable["JCTL_10"][ jctl ];
		var lcjprice_hidden  = priceTable["LCJ_10" ][ lcj  ];
	}
	if(subcount==3){
		var jectprice_hidden = priceTable["JECT_15"][ ject ];
		var jctlprice_hidden = priceTable["JCTL_15"][ jctl ];
		var lcjprice_hidden  = priceTable["LCJ_15" ][ lcj  ];
	}
    if ( jectprice < 0 || jctlprice < 0 || lcjprice < 0 )
    {
        msg.innerHTML = '<span style="color: red;">'
                      + "You can not order print or combo editions from outside the US and Canada"
                      + '</span>';
        return -1;
    }

    var y_ject = groupValue( form.JECTsub ) + 1;
    var y_jctl = groupValue( form.JCTLsub ) + 1;
    var y_lcj  = groupValue( form.LCJsub ) + 1;
    var total = jectprice * y_ject + jctlprice * y_jctl + lcjprice * y_lcj;
	 var total_hidden = jectprice_hidden * y_ject + jctlprice_hidden * y_jctl + lcjprice_hidden * y_lcj;
	// alert(total_hidden);
	 if(total_hidden){
	 	document.getElementById('jectprice_hidden').value=jectprice_hidden;
		document.getElementById('jctlprice_hidden').value=jctlprice_hidden;
		document.getElementById('lcjprice_hidden').value=lcjprice_hidden;
	 
	 }
	
    var discount=0;
   var discount_price=0;
	
    if ( subcount == 2 )
    {
	
        discount_price = <?php echo DISCOUNT_2Journals; ?> * total; // 10% discount
		discount=<?php echo DISCOUNT_2Journals_per; ?>
    } else if ( subcount == 3 ) {
        discount_price = <?php echo DISCOUNT_3Journals; ?> * total; // 15% discount
		discount=<?php echo DISCOUNT_3Journals_per; ?>
    }
    var msgtext = "Total: $" + total.toFixed(2);
    if ( discount_price >= 0 ) 
    {
        msgtext += "<br/>Discount: -" + discount+"%";
		var subtotal=total;
        total -= discount_price;
     
		if(total_hidden){
			 msgtext += "<br/>Grand total: $" + total_hidden.toFixed(2);
		} else{
			   msgtext += "<br/>Grand total: $" + total.toFixed(2);
		}
    }
    msg.innerHTML = msgtext;
    form.total.value = total.toFixed(2);
	form.subtotal.value = subtotal.toFixed(2);
	form.discount.value = discount;
	form.itemprice1.value = jectprice.toFixed(2);
	form.itemprice2.value = jctlprice.toFixed(2);
	form.itemprice3.value = lcjprice.toFixed(2);
	document.getElementById('id_subcount').value=subcount;
	
    return total; 
}

  
</script>
<?php
include 'footer.php';
?>
</body>

</html>

It calculates the total correctly. When the information is passed to orderreview and then on to be processed, the total can be different.  Here is the orderreview code:

<?php
include 'header.php';
include 'functions.php';
//print_r($_REQUEST);exit;
extract($_REQUEST);
$back_url=basename($_SERVER['HTTP_REFERER']);
echo '<h2 style="margin-top: 0;">Review your order:</h2>';
echo '<p><b>Order for:</b></p>';
echo $_POST['firstname'].' '.$_POST['lastname'].'<br>';
if ($_POST['department']!='') echo $_POST['department'].'<br>';
if ($_POST['institution']!='') echo $_POST['institution'].'<br>';
//echo str_replace($returns,'<br>',$_POST['address']).'<br>';
echo $_POST['city'].', '.$_POST['state'].' '.$_POST['zip'].'<br>';
echo $country.'<br>';
echo $_POST['phone'].'<br>';
echo $_POST['email'].'</address>';



if($id_subcount==2){
	$discount_rate=DISCOUNT_2Journals;

} elseif($id_subcount==3){
	$discount_rate=DISCOUNT_3Journals;
}else{
	$discount_rate="";

}

?>
<table border="1">
	<tr><th>Item</th><th>Base Price</br>(Single Year)</th><th>Your Price</br>(Single Year)</th><th>Length</th><th>Renewal?</th>
	 
	<?php if ($subscription_type == "institutional") { ?>
		<th>Tier</th>
	<?php
	}
	?>
	</tr>
	<?php $originalprice1 = $itemprice1 ?>
	<?php $originalprice2 = $itemprice2 ?>
	<?php $originalprice3 = $itemprice3 ?>
	
	<?php
		if($JECT && $JECT!="no"){
			if($jectprice_hidden){
				$itemprice1=$jectprice_hidden;
			} else {
				$itemprice1=$itemprice1;
			}
			
	?>
	
	
		<tr>
			<td>
	<?php
		$JECTtext = $JECT;
		switch ($JECTtext)
			{
		case "JECTindividual_print":
			echo "Journal on Excellence (JECT) Individual Print Subscription";
			break;
		case "JECTindividual_electronic":
			echo "Journal on Excellence (JECT) Individual Electronic Subscription";
			break;
		case "JECTinstitutional_electronic":
			echo "Journal on Excellence(JECT) Insitutional Subscription";
			break;
		case "JECTindividual_combo":
			echo "Journal on Excellence (JECT) Individual Combo Subscription";
			break;
		case "JECTinstitutional_print":
			echo "Journal on Excellence (JECT) Institutional Print Subscription";
			break;
		default:
			echo "Journal on Excellence (JECT) subscription";
			}
	?>
			</td>
			<td><?php echo $originalprice1; ?></td>
			<td><?php echo round($itemprice1,2); ?></td>
			<td><?php echo $JECTsub; ?></td>
			<td><?php if($JECTrenewal){ echo "Yes";} else { echo "No";} ?></td>
			<?php if ($subscription_type == "institutional") { ?>
			<td><?php echo $tier; ?></td>
		<?php
		}
		?>
		</tr>
	<?php
	}
	?>
	
	<?php
		if($JCTL && $JCTL!="no"){
			if($jctlprice_hidden){
				$itemprice2=$jctlprice_hidden;
			} else {
				$itemprice2=$itemprice2;
			}
	?>
		<tr>
			<td>
	<?php
		$JCTLtext = $JCTL;
		switch ($JCTLtext)
			{
		case "JCTLindividual_print":
			echo "Journal on Centers(JCTL) Individual Print Subscription";
			break;
		case "JCTLindividual_electronic":
			echo "Journal on Centers (JCTL) Individual Electronic Subscription";
			break;
		case "JCTLinstitutional_electronic":
			echo "Journal on Centers  (JCTL) Insitutional Subscription";
			break;
		case "JCTLindividual_combo":
			echo "Journal on Centers  (JCTL) Individual Combo Subscription";
			break;
		case "JCTLinstitutional_print":
			echo "Journal on Centers  (JCTL) Institutional Print Subscription";
			break;
		default:
			echo "Journal on Centers (JCTL) subscription";
			}
	?>
			</td>
			<td><?php echo $originalprice2; ?></td>
			<td><?php echo round($itemprice2,2); ?></td>
			<td><?php echo $JCTLsub; ?></td>
			<td><?php if($JCTLrenewal){ echo "Yes";} else { echo "No";} ?></td>
			<?php if ($subscription_type == "institutional") { ?>
			<td><?php echo $tier; ?></td>
		<?php
		}
		?>
		</tr>
	<?php
	}
	?>
	<?php
		if($LCJ && $LCJ!="no"){
			if($lcjprice_hidden){
				$itemprice3=$lcjprice_hidden;
			} else {
				$itemprice3=$itemprice3;
			}
	?>
		<tr>
		<td>
	<?php
		$LCJtext = $LCJ;
		switch ($LCJtext)
			{
		case "LCJindividual_print":
			echo "Learning Communities (LCJ) Individual Print Subscription";
			break;
		case "LCJindividual_electronic":
			echo "Learning Communities  (LCJ) Individual Electronic Subscription";
			break;
		case "LCJinstitutional_electronic":
			echo "Learning Communities (LCJ) Insitutional Subscription";
			break;
		case "LCJindividual_combo":
			echo "Learning Communities  (LCJ) Individual Combo Subscription";
			break;
		case "LCJinstitutional_print":
			echo "Learning Communities (LCJ) Institutional Print Subscription";
			break;
		default:
			echo "Learning Communities (LCJ) subscription";
			}
	?>

			</td>
			<td><?php echo $originalprice3; ?></td>
			<td><?php echo round($itemprice3,2); ?></td>
			<td><?php echo $LCJsub; ?></td>
			<td><?php if($LCJrenewal){ echo "Yes";} else { echo "No";} ?></td>
			<?php if ($subscription_type == "institutional") { ?>
			<td><?php echo $tier; ?></td>
		<?php
		}
		?>
		</tr>
	<?php
	}
	?>
</table>
	
<?php



if ($subscription_type == "institutional") {
	//$back_url="institutionalorder.php";
	echo '<p>Institution details:</p><ul>';
	if ($_POST['tech_contact']!='') echo "<li>Technical contact: ".$_POST['tech_contact'].'</li>';
	if ($_POST['tc_phone']!='') echo "<li>Technical contact's phone: ".$_POST['tc_phone'].'</li>';
	if ($_POST['tc_email']!='') echo "<li>Technical contact's email: ".$_POST['tc_email'].'</li>';
	echo "<li>Network address(es):<ul><li>".str_replace($returns,'</li><li>',$_POST['net_addresses']).'</li></ul></li>';
	echo '</ul>';
	} else {
		//$back_url="individualorder.php";
	}



echo "<h3>Subtotal: ".$_POST['subtotal'].'</br>';
echo "Discount: ".$_POST['discount']."%".'</br>';
echo "Grand total: ".$_POST['total'].'</h3>';
echo '<p>Payment method: '.$_POST['payment_method'].'</p>';
echo '<h2>Review and submit</h2>';
echo '<p>Please review your order above to make sure it is correct, then print this page as your invoice/receipt. Finally, press the "Submit order" button below. You should receive a confirmation of your order via the email address you provided. We will begin processing it once we have received your payment. Please allow a maximum 7-10 days for you payment to be processed and for your order to be carried out.</p>';

//if ($_POST['payment_method']=='Credit card (online)') echo '<p>After you have clicked the "Submit order" button below, you will be taken to our secure payment site to complete the order.  Please have your credit card ready.</p>';
echo '<table><tr>';
echo '<td>
<form action="'.$back_url.'" method="post">
<input type="submit" name="redo" value="Go back and make changes">';
echo capturepostdata($_POST);
echo '</form></td>';

echo '<td><form action="order_process.php" method="post"><input type="submit" name="submit" value="Submit order">';
echo capturepostdata($_POST);

echo '</form></td>';
echo '</tr></table>';
include 'footer.php';
?>

I will give you an example.  If the individual electronic item is ordered for each choice for a term of 2 years the price should calculate at 15%discount price x 2 for each one and then those three figures totaled (67.15*2)+(16.95*2)+(33.15*2)=234.50. That is the correct calculation and it appears on the order page as the total.  When submit is hit $234.51 appears as the total on the order review page.  I believe this is because it is just taking the subtotal*15% discount and not doing the calculation the same way.  I need the calculation to 1.  be correct in both places and 2.  calculate from the first methods (the reason for this is complicated, but it has to do with tracking the individual amounts in separate places for credit card purposes.)  Sorry if this sounds complicated - it is and I am confused mightily. Any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/280241-total-off-by-penny-cant-figure-out-how/
Share on other sites

  • Solution

the total submitted to the php code is in the total javascript variable. but you have two totals, one in total_hidden and one in total. if you use alert(total) does it give you the displayed/correct total or the incorrect/php code value?

 

[you also have TOO much code] - you apparently have an array holding the discounted amounts and you are also calculating the discount and subtracting it from the total. i suspect the error is in the two uses of values.

 

edit: more detailed answer - the total_hidden will always be true (did i mention you have TOO much code) if any subscription has been selected. total_hidden is calculated from the javascript array of values and it is what you display in the form as the Grand Total: however, the total that you submit in the form field is calculated/subtracted in the javascript code and is likely suffering from a floating point conversion error. total and total_hidden should be the same, i.e. you don't need two times the variables/code/data/calculations to do this.

Edited by mac_gyver

off topic for this thread, but the following is an example of a data driven design, where you have your product data defined somewhere (array, database table) and your main code simply loops over the products. the code is generic and there's no product specific reference in the code at all (eliminating all those long variable names to keep track of which product the value is for.) this separates the code from the data and eliminates any replicated code for each product. if you need to change anything about the products (add more, rearrange the display order, change a tile...), all you do is change the data definition. the main code isn't touched and if you need to change the html markup, it only exists once.

 

you can run the following in a .php file by itself to see how it works -

<?php
//some madeup values for testing -
$JECTindividual_print_price['base']['United States'] = 10.00;
$JECTindividual_print_price['base']['Canada'] = 11.00;
$JECTindividual_electronic_price['base'] = 5.00;
$JECTindividual_combo_price['base'] = 12.00;


// define the product choices
$product['JECT'] = array('title'=>'Journal on Excellence',
    'price_us_print'=>$JECTindividual_print_price['base']['United States'],
    'price_can_print'=>$JECTindividual_print_price['base']['Canada'],
    'price_electronic'=>$JECTindividual_electronic_price['base'],
    'price_combo'=>$JECTindividual_combo_price['base']
);
$product['JCTL'] = array('title'=>'Journal on Centers',
    // replace following with actual amount variables, these are just a copy of JECT values for demo purposes
    'price_us_print'=>$JECTindividual_print_price['base']['United States'],
    'price_can_print'=>$JECTindividual_print_price['base']['Canada'],
    'price_electronic'=>$JECTindividual_electronic_price['base'],
    'price_combo'=>$JECTindividual_combo_price['base']
);
$product['LCJ'] = array('title'=>'Learning Communities',
    // replace following with actual amount variables, these are just a copy of JECT values for demo purposes
    'price_us_print'=>$JECTindividual_print_price['base']['United States'],
    'price_can_print'=>$JECTindividual_print_price['base']['Canada'],
    'price_electronic'=>$JECTindividual_electronic_price['base'],
    'price_combo'=>$JECTindividual_combo_price['base']
);

// "carefully" use extract to populate program variables for testing -
if(isset($_POST['redo'])) {extract($_POST);}
// start of form for testing -
echo "<form method='post'>";


// produce the form fields for the product choices -
foreach($product as $key=>$prod){
echo "<font size='+1'>
<h3><b><em>{$prod['title']}</em></b></h3>
</font>
<input type='radio' name='choice[$key]' value='no' ".(!isset($choice[$key]) || $choice[$key]=="no" ? 'checked' : '')."> No thanks</br>
<input type='radio' name='choice[$key]' value='print' ".(isset($choice[$key]) && $choice[$key]=="print" ? 'checked' : '').">
Print subscription (US and Canada only): US \${$prod['price_us_print']}, Canada \${$prod['price_can_print']}</br>
<input type='radio' name='choice[$key]' value='electronic' ".(isset($choice[$key]) && $choice[$key]=="electronic" ? 'checked' : '').">
Electronic subscription (access to complete online archive): \${$prod['price_electronic']}</br>
<input type='radio' name='choice[$key]' value='combo' ".(isset($choice[$key]) && $choice[$key]=="combo" ? 'checked' : '').">
Combination print and electronic subscription (US and Canada only): \${$prod['price_combo']}</br>
<p style='text-indent:30px;'>Length of subscription:</br></p>
<label><input type='radio' name='sub[$key]' value='1' ".(!isset($sub[$key]) || $sub[$key]=="1" ? 'checked' : '')."> 1 year</label></br>
<label><input type='radio' name='sub[$key]' value='2' ".(isset($sub[$key]) && $sub[$key]=="2" ? 'checked' : '')." > 2 years</label></br>
<label><input type='radio' name='sub[$key]' value='3' ".(isset($sub[$key]) && $sub[$key]=="3" ? 'checked' : '')."> 3 years</label></br>
<label><input type='checkbox' name='renewal[$key]' value='yes' ".(isset($renewal[$key]) && $renewal[$key]=="yes" ? 'checked' : '')."> Check box if this is a renewal</label></br>
";
}


// submit button for testing -
echo "<input type='submit' name='redo'></form>";
?>

because the form fields have been changed to array names, any code referencing them would need to be changed to match.

 

since the data is submitted as arrays to the form processing code, that code would simply loop over the data, not repeat the blocks of code for each different product.

I'm not going to bother with reading through all that code (as mac_gyver stated, you have too much code). But, I will provide this:

 

1. If you need to do a calculation (such as for a shopping cart), create ONE process to do that calculation. Never rewrite the logic to do something.

 

2. NEVER rely upon any client-side calculations or validations. It is fine to use them to give the user some quick feedback - but don't rely on them. You should, typically, create your pages to not use any JS validations. Once everything is working - then you can add them in on top of any server-side validations you need.

 

In this case, I would not use JS at all to do any cart calculation. I would force the user to submit a page to see the total. Or, at the very least, use AJAX to make a server-side call to do the calculation.

Thanks Psycho, for the helpful tips.  And of course, big thanks to mac_gyver.  I consider you a genius and wish you were my personal Yoda. It would make learning php a lot easier.  You were on spot.  Once this is working, I am going to go back and see if I can weed it - there is way too much. 

Slowly learning how to crawl...

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.