Jump to content

Recommended Posts

Hi, I am new to php and really need some help & advise. I am using a basic php script where a customer adds a promo code in a form which then redirects them to a special ordering page. We have a larger promotion coming up where I need to provide a much larger number of one-off codes and I didn't want to have to write hundreds of lines of code with these unique promo codes.

I was wondering if there was a way to check if the promo code is correct against the codes in the MySQL database and then if correct redirect the customer to the correct URL?

Thanks, any help will be welcome :)

The simple code I have used so far is:

<?php

$errmsg = "";

if(empty($_POST['pode'])) {

$errmsg = 'Enter promotional code';

include 'promoform.php';

 

} else {

switch($_POST['pcode']) {

 

case "code123":

$redirectpage = 'http://www.website.com';

 

break;

 

case "code456":

$redirectpage = 'http://www.website.com';

}

header("Location: $redirectpage");

}

?>

Sure there is. I'm not sure if you want a different page per code or not, but the following is assuming different pages. First thing would be to make sure your DB is setup right so that it includes Offer Code, Status and URL.

Then you have your script SELECT the url that is attached to the offer code

 

SELECT url FROM offerTable WHERE offerCode = :oCode AND status = "active"

Then sense-check the results to make sure it's a valid code and a valid response from the DB then just use the returned URL to send people where you want them to go.  It also completely removes the need for your CASE.

 

Have a bash and let us know how you get on.

 

Note: the Status allows you to enable and disable offer codes, but if you are doing this you would want to set up some form of persistence.

Another option, instead of the "active" flag is to hold "valid_from" and "valid_to" dates, then

SELECT url FROM offerTable 
WHERE offerCode = :oCode 
     AND CURDATE() BETWEEN valid_from AND valid_to

 

Another option, instead of the "active" flag is to hold "valid_from" and "valid_to" dates, then

SELECT url FROM offerTable 
WHERE offerCode = :oCode 
     AND CURDATE() BETWEEN valid_from AND valid_to

 

I think we've been involved with more dates on here today than I've had with my wife! lol

 

To keep the theme you could also add "issued date" and "redeemed date" fields which could help a lot with sales metrics later on.  :happy-04:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.