PHP Code Not Redirecting

Hi All,
I have the below code which redirects to specific URLs once a code is entered on our website. If the code is incorrect it taking the customer to a ‘This Page cannot be displayed’ which is not correct. I really want the customer to stay on the page with the promo box with an error message saying incorrect code - this is not happening, any idea why?
Thanks in advance :slight_smile:

[php]<?php

$errmsg = “”;

if(empty($_POST[‘pode’])) {

$errmsg = ‘Enter promotional code’;

} else {

switch($_POST[‘pcode’]) {

case “code123”:

$redirectpage = ‘http://www.website.com’;

break;

case “code456”:

$redirectpage = ‘http://www.website.com’;

}

header(“Location: $redirectpage”);

}

?>[/php]

[php]<?php
/* Set $pcode variable if it is set, if not set it to NULL */
$pcode = isset($_POST[‘pcode’]) ? trim($_POST[‘pcode’]) : NULL;
$errmsg = “”;
$redirectpage = NULL; // Set it to null (false):

/* check $pcode variable for corresponding code and assign redirect variable */
switch ($pcode) {
case “code123”:
$redirectpage = ‘http://www.website.com’;
break;
case “code456”:
$redirectpage = ‘http://www.website.com’;
break;
default :
$errmsg = ‘Enter promotional code’;
}

/* Check to see if it is TRUE else it is NULL(FALSE) */
if ($redirectpage) {
header("Location: " . $redirectpage);
exit(); // Should always use an exit statement for it’s good coding practice:
} else {
echo $errmsg . “
\n”;
}[/php]

A helpful suggestion - use an IDE (A good IDE will either give you a warning or give you a suggestion) or pay really good attention to detail to your variable naming for you had $_POST[‘code’] and $_POST[‘pcode’] ----> They aren’t the same.

Perfect, thanks! :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service