Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #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>&pound;<?php echo  $amount; ?></h3>
    Thanks!
    Last edited by Jodzzz; 11-20-2012 at 03:34 PM. Reason: Problem solved!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 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:

    Jodzzz (11-20-2012)

  • #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?

  • #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?

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    It would replace your foreach.

  • #6
    New Coder
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #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?

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 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:

    Jodzzz (11-20-2012)

  • #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

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 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
    New Coder
    Join Date
    Nov 2012
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks alot man all sorted!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •