...

View Full Version : Resolved Arrays & Displaying Messages



Jodzzz
11-20-2012, 01:32 PM
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



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



<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!

Fou-Lu
11-20-2012, 02:40 PM
You don't need to loop at all to do this. A simple isset check will work:


if (!isset($arr[$_POST['couponCode']]))
{
printf('%s is not a valid code.', $_POST['couponCode']);
}
else
{
$discount = $arr[$_POST['couponCode']];
$amount -= $discount['discount'];
}

Jodzzz
11-20-2012, 02:43 PM
Where would i put that in my code?

where i wanted the error message to be displayed?

Jodzzz
11-20-2012, 02:45 PM
i've put it in but it displays is not a valid code all the time, how would i have that completly hidden?

Fou-Lu
11-20-2012, 02:49 PM
It would replace your foreach.

Jodzzz
11-20-2012, 03:16 PM
i don't understand, sorry.

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

if you look at that you will see what i mean.

Jodzzz
11-20-2012, 03:19 PM
Also could i have it say? your discountwas applyed if they entered a correct code?

Fou-Lu
11-20-2012, 03:42 PM
Yeah you simply add that in the else:


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]

Jodzzz
11-20-2012, 03:45 PM
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

Fou-Lu
11-20-2012, 04:01 PM
Oh yeah I guess you need to check that its been posted first.


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');
}
}

Jodzzz
11-20-2012, 04:30 PM
Thanks alot man all sorted!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum