Enjoy an ad free experience by logging in. Not a member yet?
Register .
11-20-2012, 12:32 PM
PM User |
#1
New Coder
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Arrays & Displaying Messages
Hi Guys,
I'm doing a paypal discount coupon thing. Its all working fine there's just one thing I'm missing.
If a code is entered which isn't in the array, i want it to post a message saying "The code you entered was incorrect"
Simple as that!
Here's the code
Code:
<?php
header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
header('Pragma: no-cache'); // HTTP 1.0.
header('Expires: 0'); // Proxies.
$hostedButtonId = "FVSQDMQQC3K3N";
$amount = 10.99;
$arr = array(
"MFM" => array("hostedButtonId" => "S42XZMR6QW8VJ", "discount" => "1.10"),
"BU10KZ" => array("hostedButtonId" => "SCS9JZBDFUW4Q", "discount" => "1.10"),
"XZ10D2" => array("hostedButtonId" => "C9LK8DMEX3AJ6", "discount" => "1.10"),
"BP15D3" => array("hostedButtonId" => "EJENJCYB6TEQ4", "discount" => "1.65");
foreach ($arr as $key => $value) {
if ($key == $_POST["couponCode"])
{
$hostedButtonId = $value["hostedButtonId"];
$discount = $value["discount"];
$amount = ($amount - $discount);
break;
}
}
?>
and
Code:
<form action="buy-now.php" method="post" >
<input type="text" size="6" name="couponCode" placeholder="enter code" style="margin-bottom:10px;" >
<input type="submit" name="submitCouponCode" value="Apply Discount">
</form>
<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="<?php echo $hostedButtonId; ?>">
<input type="image" src="http://www.beltupkidz.com/img-bin/buy-now-blue.jpg" border="0" name="submit" alt="PayPal — The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>
<h3>£<?php echo $amount; ?></h3>
Thanks!
Last edited by Jodzzz; 11-20-2012 at 03:34 PM ..
Reason: Problem solved!
11-20-2012, 01:40 PM
PM User |
#2
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
You don't need to loop at all to do this. A simple isset check will work:
PHP Code:
if (!isset( $arr [ $_POST [ 'couponCode' ]])) { printf ( '%s is not a valid code.' , $_POST [ 'couponCode' ]); } else { $discount = $arr [ $_POST [ 'couponCode' ]]; $amount -= $discount [ 'discount' ]; }
Users who have thanked Fou-Lu for this post:
11-20-2012, 01:43 PM
PM User |
#3
New Coder
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Where would i put that in my code?
where i wanted the error message to be displayed?
11-20-2012, 01:45 PM
PM User |
#4
New Coder
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
i've put it in but it displays is not a valid code all the time, how would i have that completly hidden?
11-20-2012, 01:49 PM
PM User |
#5
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
It would replace your foreach.
11-20-2012, 02:19 PM
PM User |
#7
New Coder
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Also could i have it say? your discountwas applyed if they entered a correct code?
11-20-2012, 02:42 PM
PM User |
#8
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Yeah you simply add that in the else:
PHP Code:
if (!isset( $arr [ $_POST [ 'couponCode' ]])) { printf ( '%s is not a valid code.' , $_POST [ 'couponCode' ]); } else { $discount = $arr [ $_POST [ 'couponCode' ]]; $amount -= $discount [ 'discount' ]; print( 'The discount has been applied' ); }
I assumed as well that this is a part of the same page. $amount will only carry worth if it is located somewhere below where the $amount is subtracted from. Given that this is in HTML, you'll want to simply wrap the output from the prints into a div or paragraph tag so you can shape it with the rest of the output.
[/php]
Users who have thanked Fou-Lu for this post:
11-20-2012, 02:45 PM
PM User |
#9
New Coder
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
One last thing, how do i go about hiding "is not a valid code" untill an invalid code is entered?, at the moment its displaying all the time
11-20-2012, 03:01 PM
PM User |
#10
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Oh yeah I guess you need to check that its been posted first.
PHP Code:
if (isset( $_POST [ 'couponCode' ])) { if (!isset( $arr [ $_POST [ 'couponCode' ]])) { printf ( '%s is not a valid code.' , $_POST [ 'couponCode' ]); } else { $discount = $arr [ $_POST [ 'couponCode' ]]; $amount -= $discount [ 'discount' ]; print( 'The discount has been applied' ); } }
11-20-2012, 03:30 PM
PM User |
#11
New Coder
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks alot man all sorted!
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 11:14 AM .
Advertisement
Log in to turn off these ads.