Forums

Validate a Promo Code

dw1 15 Sep, 2010
Hi

Looking to add a "Promo Code" field in our form.
Tried without success to validate a field "promo_code".

Every month we will have a unique code that users will enter. ie: 123456 for Jan, 135791 for Feb.
How do you validate the specific code?

Thanks.

Dave
GreyHead 16 Sep, 2010
Hi Dave,

You don't say what you want to happen if the code is invalid. Assuming that you want to reject the form then you could do this in the Sever-side validation box:
<?php
$promo_code = JRequest::getString('promo_code', '', 'post');
if ( !$promo_code || $promo_code != 'ABCD' ) {
  return "Sorry, that's an invalid promo code";
}
?>


Bob
dw1 16 Sep, 2010
Hey Bob

Thanks for the code.
Unfortunately, it's not working. Parse Error coming up on front end at top of form.
Thought it might be a misplaced ";" somewhere but I'm really not sure. Tried a few options of editing your code but nothing worked.

Here's what I have so far.

Parse error: syntax error, unexpected T_RETURN in /components/com_chronocontact/libraries/chronoform.php(258) : eval()'d code on line 8



 <?php
if($_POST['accept_terms'] != 'yes')
return 'Sorry, but you need to accept our terms to proceed';
?>
<?php
$promo_code = JRequest::getString('promo_code', '', 'post');
if ( !$promo_code || $promo_code != 'ABCD' ) {
  return "Sorry, that's an invalid promo code";
}
?>
dw1 16 Sep, 2010
Got the code to work, however how do you allow form submittal when a user will not use a promo code.
With this code the error message is populating when this field is empty. Only some users will be entering in the promo code. Some users will enter nothing in this field. Hope that's clear.

-Dave


----Updated code----
<?php
 $promo_code = JRequest::getString('promo_code', '', 'post');
if (!$promo_code || $promo_code != 'ABCD')
      return "Sorry, that's an invalid promo code";
?>


Took the spaces out next to the "()" in line #3.
GreyHead 17 Sep, 2010
Hi dw1,

Just change the logic a little:
<?php
$promo_code = JRequest::getString('promo_code', '', 'post');
if ( $promo_code && $promo_code != 'ABCD' ) {
      return "Sorry, that is an invalid promo code";
}
?>


Bob

PS I'm not sure what the error was (it's not the spaces) it may have been the ' in the return messages
dw1 21 Sep, 2010
Bob

All is working with your new code. Really appreciate it!
I had similar problems with the code at first. Was coming up as error once posting form.
I removed the spaces and line breaks and replaced them. Perhaps when copying it brought in something that was not what it should be.

Thanks.
-Dave
This topic is locked and no more replies can be posted.