Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help needed checking if coupon code has been used

Posted on 2014-03-20
2
Medium Priority
?
725 Views
Last Modified: 2014-03-25
I'm trying to check if a coupon code is valid and has not been used. My first step is to check the couponCode value against the registeredCodesAr array to make sure the code exists. If the code exists, I then want to make sure it has not been used by looking for it in the table.

The table I'm testing against has code01 and codepm01, but not codepm02.

Unfortunately though, it doesn't seem to be working. I've narrowed down the problem to my PHP, and was hoping someone might be able to see where my mistake is.

paycode.php
$registeredCodesAr = array('Code01', 'CodePM01', 'CodePM02');
$registeredCodes = array_map('strtolower', $registeredCodesAr);

$requestedCode  = mysql_real_escape_string(htmlentities(strtolower($_REQUEST['couponCode'])));

if( in_array($requestedCode, $registeredCodes) ){

	$result = mysql_query("SELECT couponCode FROM requests WHERE couponCode = $requestedCode");

	if(mysql_num_rows($result) > 0) {
     	echo 'false'; // Code has been used
	} else {
   		echo 'true'; // Code has not been used
	}
} else {
   		echo 'true'; // Code doesn't exist
}

Open in new window

Portion of HTML/Script
<script type="text/javascript">

$(document).ready(function() {

	$("#frm_contact").validate({
		rules: {
			couponCode: {
				required: true,
				remote: "paypal-express-checkout/paycode.php",
			}					
		},
		messages: {
			couponCode: "This code has been used."
		}		
	});

});
</script>
<form name="frm_contact" id="frm_contact" method="post" action="">
<input type="text" class="ft" name="couponCode" id="couponCode" />
</form>

Open in new window

0
Comment
Question by:jej07
2 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1600 total points
ID: 39944082
If you're new to PHP and want to get a foundation in how all of this works, you might find some good learning resources here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

The general strategy would be to keep a data base table of coupon codes.  When each code was used, you would remove it from the table.  A query that looked for the coupon code in the table would return either one or zero rows.  If there is a row, the coupon code has not been used.  The column for the coupon code would probably be marked UNIQUE to prevent duplicate codes from accidentally "giving away the store."  This is an area where marketing, merchandising and business rules determine the technical solution.  Some promotions would be loss leaders with "one coupon per customer" and some would be "clearance" to encourage as many sales as possible.

You might also have an alias name for the coupon codes (not unique) that would allow you to associate the coupons with a particular marketing campaign.  It would say something like "50 cents off orange juice."  This would be the thing that the human customer would see and associate with the promotion.  The coupon code would be a unique number, barcoded into the coupon document.

You're going to be facing a data base conversion soon.  This article explains why and what you must do to keep your web applications running.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 400 total points
ID: 39944501
Agree with Ray.
That said, here guidelines to post question here at EE: you should specify what does it mean 'it doesn't seem to be working': do you receive any error message? What is the output of your script? Alway true? Always false?
Please, place at the top of php script these two lines

<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');

And post here any error message you get. Be more descriptive about 'not working' actuallly means so we can figur out where is the problem.

Finally: your php script returns true even if the code doesn't exist: why? IMHO, the logic shouldb:

if code doesn't exist -> false
if code has been used yet -> false
else -> true
$registeredCodesAr = array('Code01', 'CodePM01', 'CodePM02');
$registeredCodes = array_map('strtolower', $registeredCodesAr);

$requestedCode  = mysql_real_escape_string(htmlentities(strtolower($_REQUEST['couponCode'])));

if( in_array($requestedCode, $registeredCodes) ){

	$result = mysql_query("SELECT couponCode FROM requests WHERE couponCode = $requestedCode");

	if(mysql_num_rows($result) > 0) {
     	echo 'false'; // Code has been used
	} else {
   		echo 'true'; // Code has not been used
	}
} else {
   		echo 'false'; // Code doesn't exist
}

Open in new window


Cheers
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question