Jump to content

discount issue - I've lost my mind.


turpentyne

Recommended Posts

I have no idea how to fix this problem. because I'm not even sure where the problem's coming from.

 

The user completes a multi-page form. When they get to register2.php, they enter a discount code. register3.php takes that code, checks the database to see if it exists, and/or is active. But for some reason, when they enter a non-existent discount code, "BREAK" it doesn't work. There are a couple of other nonexistent codes that cause the same problem. What's happening is that the Grand_total and Final_total are zeroing out which makes the user get kicked to register4free.php because it thinks they've registered for a $0.00 /free class.  If they enter no discount, or an existing discount, the dollar amount does what it's supposed to.  ANd

 

Here's the page:

 

<?php


/* start the session */
session_start();


// connect to database 
include("dbconnect.php"); 
$discount = mysql_escape_string($_POST['discount']); // this is the part causing problems.
$donation = mysql_escape_string($_POST['donation']); //just a donation. irrelevant.



$total = mysql_escape_string($_POST['total']); // the total cost of the class from previous page. discount is subtracted below
    


/* start insert of math */
$discount_code = mysql_escape_string($_POST['discount']); //The discount code, if entered.

    //$selected_classes = array(217 => 1, 215 => 2); // i.e. How many of each class was selected. class_id => quantity
    $selected_classes = $_SESSION['s_filtered'];

$grand_total = "";

    // Built an array with information about each class, and what discount it can get.
    $classes = array();
    foreach ($selected_classes as $class => $quantity)
    { $testquery = "SELECT * FROM `active_discounts` LEFT JOIN tbl_discount ON active_discounts.disc_id = tbl_discount.discount_id WHERE `code` = '{$discount_code}' AND `class_id` = '{$class}'";
    
        $result = mysql_query($testquery);
    
        if (mysql_num_rows($result) > 0)
            {
            
            $discount_row = mysql_fetch_assoc($result);
            $discount_type = $discount_row['discount_type'];
            $discount_value = $discount_row['discount_amount'];
            $discount_name = $discount_row['discount_name'];
		$active_inactive = $discount_row['active_inactive'];

            $result = mysql_query("SELECT * FROM `tbl_workshops` WHERE `workshop_id` = '{$class}'");
            $class_row = mysql_fetch_assoc($result);
            $class_name = $class_row['workshop_title'];
            $class_cost = $class_row['workshop_price'];
            $classes[$class]['workshop_title'] = $class_name;
            $classes[$class]['workshop_price'] = $class_cost;
		$classes[$class]['active_inactive'] = $active_inactive;
            
            $classes[$class]['discount_name'] = $discount_name;
            $classes[$class]['discount_type'] = $discount_type;
            
            $classes[$class]['discount_value'] = $discount_value;
            $classes[$class]['quantity'] = $quantity;
            }
            // The section below was supposed to be for when there was no discount. (nothing found in the active_discounts table) may be what's not working?
        else {
            $discount_value = "0";
             $result = mysql_query("SELECT * FROM tbl_workshops WHERE workshop_id = '{$class}'");
            if (mysql_num_rows($result) > 0){
                $row = mysql_fetch_assoc($result);
                $grand_total += $row['workshop_price'] * $quantity;
                
            }
            else {
                // The class doesnt exist
            }
            }
            
        }
    // setting some constants to make the code more readible.   
define("OFF_TOTAL", 1);
    define("OFF_INDIVIDUAL", 2);
    define("PERC_TOTAL", 3);
    define("PERC_INDIVIDUAL", 4);
    define("OFF_FRIEND", 5);
    define("PERC_FRIEND", 6);
    
    // Now looping through each class to take off any valid discounts
    
    foreach ($classes as &$class)
        {
        if ($class['discount_type'] == OFF_INDIVIDUAL)
            {
            $total = $class_cost * $class['quantity'];
            $discount = $class['quantity'] * $class['discount_value'];
            $total = $total - $discount;
            $grand_total += $total;
            
            }
    
        if ($class['discount_type'] == PERC_INDIVIDUAL)
            {
            $total = $class_cost * $class['quantity'];
            // did say  $total = $class['cost'] * $class['quantity'];
            $discount = ($class['discount_value'] / 100) * $class_cost;
            $total = $total - $discount;
            $grand_total += $total;
            }
    
    
        if ($class['discount_type'] == OFF_FRIEND)
            {
            $firstclass = $class_cost-$class['discount_value'];
    $total = $class_cost * $class['quantity'] - $class['discount_value'];
    $grand_total += $total;
            }
    
if ($class['discount_type'] == PERC_FRIEND)
    {
    $discount = ($class['discount_value'] / 100) * $class_cost; 
    $total = $class_cost * $class['quantity'] - $discount;
    $grand_total += $total;
    }
    
        }

    if($discount_type == PERC_TOTAL)
        {
$total = mysql_escape_string($_POST['total']);
        $discount = ($class['discount_value'] / 100) * $total;
        $grand_total = $total - $discount;
        
        }

    if ($class['discount_type'] == OFF_TOTAL)
        {
        $discount_value = $class['discount_value'];
$grand_total = ($class_cost * $class['quantity'])-$discount_value;
        }



    


$_SESSION['s_total_price'] = $grand_total;

// here's where it's kicking them to another page because the total is zero.
if ($_SESSION['s_total_price'] == 0) {
$_SESSION['$pay_or_not'] = "1";
header("Location: register4free.php"); 
echo $grand_total;

}	


/* end insert of new math */
?>	

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!--[if !IE]><!-->
<link rel="stylesheet" type="text/css" href="css/master3.css"/>
<!--<![endif]-->

<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="css/master3_ie9.css"/><![endif]-->

<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="css/master3_ie9.css"/><![endif]-->

<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="css/master3_ie9.css"/>
<![endif]-->

<!--[if IE 9]>
<link rel="stylesheet" type="text/css" href="css/master3_ie9.css"/>
<![endif]-->



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="ROBOTS" content="NOINDEX, NOFOLLOW">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="Description" />
<meta name="keywords" content="Keywords" />
<meta name="author" content="workshopsaz.org" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Workshops for Youth and Families - Building character.  Inspiring confidence.</title>

<script type="text/javascript">

function showMe(id){
document.getElementById(id).style.visibility='visible'
}

timer=null

function hideMe(){
clearTimeout(timer)
timer=setTimeout("document.getElementById('div_name').style.visibility='hidden'",500)
}

</script> 

<script type="text/javascript">function randRange(data) {
    	var newTime = data[Math.floor(data.length * Math.random())];
    	return newTime;
    }
    
    function toggleSomething() {
    	var timeArray = new Array(200, 300, 150, 250, 2000, 3000, 1000, 1500);
    //	do stuff
    	$("#orho1").toggleClass("visible");
    //	end stuff
        clearInterval(timer);
        timer = setInterval(toggleSomething, randRange(timeArray)); 
    }
    
    var timer = setInterval(toggleSomething, 1000);
    // 1000 = Initial timer when the page is first loaded</script>

    </script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script type="text/javascript"> // initialise variable to save cc input string
var ccnumber_saved = "";

function checkLuhn(input) {
var sum = 0;
var numdigits = input.length;
var parity = numdigits % 2;
for(var i=0; i < numdigits; i++) {
	var digit = parseInt(input.charAt(i))
	if(i % 2 == parity) digit *= 2;
	if(digit > 9) digit -= 9; sum += digit;
	} return (sum % 10) == 0; }

</script>


</head>

<div id="containermain">
   <? include("header.php"); ?>

<div id="div_name" style="visibility:hidden;solid #aaa; z-index:5000;height:0px;position:relative;top:-96px;left:-55px;"><div style="background-color:#ffffff;width:100px;z-index:5001;padding:9px;-moz-border-radius: 17px;
-webkit-border-radius: 17;border-radius: 17px;">
Hi! I'm ORHO! <br>Find out more about me here...</div>
</div>


      <p><!-- start central content area -->
      <img src="images/head/head_reg4.gif" style="position:relative;left:-43px;top:5px;"/><br>
      <font face="georgia" size=5em><b>Step 4 - Credit Card Info</b></font><br><br>
      <form method="post" class="registration_form" action="register4.php" onsubmit="return myForm()"><br>
      <table border="0">
      
      <?php


$final_total = ($final_total + $donation);
$_SESSION['s_total'] = $final_total;
$reg_id = $_POST['reg_id'];
$fname = mysql_escape_string($_POST['fname']);
$lname = mysql_escape_string($_POST['lname']);
$address = mysql_escape_string($_POST['address']);
$address2 = mysql_escape_string($_POST['address2']);
$city = mysql_escape_string($_POST['city']);
$state = mysql_escape_string($_POST['state']);
$zip = mysql_escape_string($_POST['zip']);
$phone = mysql_escape_string($_POST['phone']);
$fax = mysql_escape_string($_POST['fax']);
$email = mysql_escape_string($_POST['email']);
$hear = mysql_escape_string($_POST['hear']);
$how1 = mysql_escape_string($_POST['how1']);
$how2 = mysql_escape_string($_POST['how2']);
$how3 = mysql_escape_string($_POST['how3']);
$how4 = mysql_escape_string($_POST['how4']);
$discount_name = mysql_escape_string($_POST['discount']);

$how = $how1 . $how2 . $how3 . $how4;
		$query_insertItem = "UPDATE tbl_registration set reg_total_price = '$final_total', discount_used = '$discount_name' where reg_id=$reg_id";
		$dberror = "";
		$ret = mysql_query($query_insertItem);

?>


<tr><td><?php if(!empty($_POST['discount'])){ if($discount_type == 4 || $discount_type == 3 || $discount_type == 6){echo "%";} else{echo "$";} echo " Discount: ";} ?> </td><td><?php  if($active_inactive == 1){echo $discount_value;} elseif($discount_code !="") {echo "<font color='#990000'>inactive/invalid discount code</font>";} ?></td></tr>
<tr><td><?php if(!empty($_POST['donation'])){ echo "Donation:";}?> </td><td> <?= $donation ?></td></tr>
<tr><td>Total: </td><td>$<?= grand_total ?></td></tr>
<tr><td>Credit Card: </td><td>

</td></tr>
<!-- save input string and strip out non-numbers -->
<!-- restore saved string -->
<tr><td>

Credit Card #:</td><td><input type="text" name="ccnumber" id="ccnumber" size="46" onblur="ccnumber_saved = this.value; this.value = this.value.replace(/[^\d]/g, ''); if(!checkLuhn(this.value)) { alert('Sorry, that is not a valid number - please try again!'); this.value = ''; }this.className='reg_off';" onfocus=" if(this.value != ccnumber_saved) this.value = ccnumber_saved;this.className='reg_live'; ">


</td></tr>
<tr><td>Expiration:</td><td>




</td></tr>
<tr><td>Card Verification:</td><td><input type="text" name="cc_code" id="cc_code" size="4" onfocus="this.className='reg_live';" onblur="this.className='reg_off';" ><font size=2> (the 3 digits on back of card)</font></td></tr>
<tr><td>Name on Card:</td><td><input type="text" name="cc_name" id="cc_name" size="46" onfocus="this.className='reg_live';" onblur="this.className='reg_off';" ></td></tr>
</table><br /><br />

      
      
      <!-- end central content area -->





	</div> </p></div>
</div>
<div class="clearfooter"></div>
  
</div><!—End Container—>

<div id="footer"><? include("footer.html"); ?></div>

Link to comment
https://forums.phpfreaks.com/topic/263365-discount-issue-ive-lost-my-mind/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.