Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-20-2012, 12:32 PM   PM User | #1
Jodzzz
New Coder

 
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Jodzzz is an unknown quantity at this point
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>&pound;<?php echo  $amount; ?></h3>
Thanks!

Last edited by Jodzzz; 11-20-2012 at 03:34 PM.. Reason: Problem solved!
Jodzzz is offline   Reply With Quote
Old 11-20-2012, 01:40 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'];

Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Jodzzz (11-20-2012)
Old 11-20-2012, 01:43 PM   PM User | #3
Jodzzz
New Coder

 
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Jodzzz is an unknown quantity at this point
Where would i put that in my code?

where i wanted the error message to be displayed?
Jodzzz is offline   Reply With Quote
Old 11-20-2012, 01:45 PM   PM User | #4
Jodzzz
New Coder

 
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Jodzzz is an unknown quantity at this point
i've put it in but it displays is not a valid code all the time, how would i have that completly hidden?
Jodzzz is offline   Reply With Quote
Old 11-20-2012, 01:49 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
It would replace your foreach.
Fou-Lu is offline   Reply With Quote
Old 11-20-2012, 02:16 PM   PM User | #6
Jodzzz
New Coder

 
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Jodzzz is an unknown quantity at this point
i don't understand, sorry.

http://client4.cnxdevserver.co.uk/be...dz/buy-now.php

if you look at that you will see what i mean.
Jodzzz is offline   Reply With Quote
Old 11-20-2012, 02:19 PM   PM User | #7
Jodzzz
New Coder

 
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Jodzzz is an unknown quantity at this point
Also could i have it say? your discountwas applyed if they entered a correct code?
Jodzzz is offline   Reply With Quote
Old 11-20-2012, 02:42 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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]
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Jodzzz (11-20-2012)
Old 11-20-2012, 02:45 PM   PM User | #9
Jodzzz
New Coder

 
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Jodzzz is an unknown quantity at this point
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
Jodzzz is offline   Reply With Quote
Old 11-20-2012, 03:01 PM   PM User | #10
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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');
    } 

Fou-Lu is offline   Reply With Quote
Old 11-20-2012, 03:30 PM   PM User | #11
Jodzzz
New Coder

 
Join Date: Nov 2012
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Jodzzz is an unknown quantity at this point
Thanks alot man all sorted!
Jodzzz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:14 AM.


Advertisement
Log in to turn off these ads.