0

I printed postcards with a coupon code saying "get 10% off if you enter coupon code COMEAGAINSOON"

Unfortunately for me, upon setting up the coupon code via my shopping cart (PDG), I find that the max length of coupon codes (not documented) is 11 characters. So I need to make the user's entry "COMEAGAINSOON" be submitted to the server as "COMEAGAINSO"

I figure there must be (hopefully) a js or other solution that does this, rather than reprinting my postcards. It's basically like: I made a bad/stupid mistake at work and am trying to fix it before I have ended up costing my company a lot of money and (perhaps) my job with them.

I've tried a number of solutions I've found here (replacing the innerhtml, shorten string), but none seem to submit that changed data directly to the right place. Also, I simply am not particularly advanced at coding, to say the least (I'm one of those designers who ends up doing web work when necessary). MaxLength is a thing, but I think it would frustrate users to not be able to enter the full coupon code they were told to use.

Thoughts appreciated. Thank you very much!!

PS - I am aware that this is a fairly basic dilemma, and that the community here is pretty advanced, and that perhaps this isn't the right place for this question. But I need to throw it out there.

<form method="post" action="/cgi-bin/redirect.cgi">
<input type="hidden" name="goto" value="/pottedstore/potted.cgi?display">
Potted Coupon Code:
<input size="17" type="text" name="pricing" value="">
<input name="submit" type="image" value="submit" src="/includes/images/submit.jpg" align="bottom" >
</form>
000
  • 26,951
  • 10
  • 71
  • 101
  • So what do you know? JavaScript? PHP or any other server side language? Can you show what you have tried so far? – putvande Sep 09 '13 at 19:55
  • Check out this stackoverflow answer: http://stackoverflow.com/questions/6912197/change-value-of-input-then-submit-form-in-javascript and http://stackoverflow.com/questions/1301512/truncate-a-string-straight-javascript – Matthew Sep 09 '13 at 19:56
  • 1
    What is your backend? You can simply change it there! – Sudipta Chatterjee Sep 09 '13 at 19:59
  • @Luke, you might want to change the coupon code to *EXAMPLE* or something. – TheFrost Sep 09 '13 at 20:03
  • I would **highly** recommend doing this server side. You cannot guarantee a user has JS enabled. Do it somewhere you can control properly. – gvee Sep 09 '13 at 20:08
  • Backend would be ideal, it's one of those situations where I don't have control over the database side of things. I am teaching myself javascript and work a little with php. I would show what I've tried but it all kind of is sad and thus depressing. And it didn't work. Joe's answer worked. Have to say: thanks to this community, you don't waste time! Sorry if I wasted yours but perhaps this will be helpful to someone. – Luke Geniella Sep 09 '13 at 20:23
  • I wish there was ANY control over the programming side of things in PDG Commerce. Unfortunately the cart is written in C (!) and it's pretty much an opaque (and obsolete and poorly written) black box. – JAL Nov 17 '13 at 22:40

3 Answers3

0
<form method="post" action="/cgi-bin/redirect.cgi" onsubmit="this.pricing.value=this.pricing.value.substring(0,11)">
000
  • 26,951
  • 10
  • 71
  • 101
  • This will trim the last character. What if a customer entered 20 characters? Wouldn't it be better if it trimmed it to 11 characters? – gvee Sep 09 '13 at 20:07
  • @gvee This does trim it to 11 characters. – 000 Sep 09 '13 at 21:02
  • @JoeFrambach apologies, I could have sworn it wasn't that when I first commented! – gvee Sep 09 '13 at 21:22
0

I'm not very versed in HTML, but if you can run a very short javascript that can pass the value on to your server, the method I would use would be the Javascript slice() method.

Say you have a string:

var string = "This is a test string";

And you use this code on said string, declaring "str" to be your new sliced string (I'm using multiple iterations to demonstrate the bounds):

var str1 = string.slice(0,5);
var str2 = string.slice(1,9);
var str3 = string.slice(3,11);
var str4 = string.slice(1,7);

Then, str1 through str4 will be as follows:

str1 = "This "
str2 = "his is a"
str3 = "s is a t"
str4 = "his is"

In your situation, I would recommend the following snippet:

var couponCode = "COMEAGAINSOON";
var clippedCoupon = couponCode.slice(0,11);

Result:

clippedCoupon = "COMEAGAINSO"

More information can be found here. Best of luck to you.

jwarner112
  • 1,492
  • 2
  • 14
  • 29
0

There are multiple places to tackle this problem: Client-Side, Server-Side, API-Side.

A Client-side solution is easy to implement and will work in the majority of cases. It will not work when the user has intentionally turned off JavaScript or is using a browser that doesn't support JavaScript. Here's an example of how to do this in JavaScript:

JavaScript:

window.onload = function () { // so it runs once the DOM is loaded
    var form = document.forms[0]; //assuming you only have one form on the page
    form.onsubmit = function () { // when the form is submitted
        var pricing = form.pricing, // cache the DOM lookup for the input
            couponCode = pricing.value; // cache the value
        if (couponCode.length > 11) { // if the value has more than 11 characters
            couponCode = couponCode.substr(0, 11); // keep the left-most 11 characters
            pricing.value = couponCode; // set the input value to the coupon code
        }
    };
};

Server-side is, likewise, simple enough to implement and works in every case. Here are some examples in PHP and Perl (as you're posting to redirect.cgi):

PHP:

$couponcode = $_REQUEST["pricing"]
if (strlen($couponcode) > 11) {
    substr($couponcode, 0, 11)
}

PERL:

my $couponcode = $q->param('pricing');
if (length($couponcode) > 11) {
    substr($couponcode, 0, 11)
}

That leaves API-side which is neither simple nor quick to implement (commonly) and should work in all cases. Since you're paying for a product (PDG), you could work with the makers of PDG to accept longer coupon codes which may help other customers suffering similar problems. This will take the longest to implement but it is not a band-aid fix like the examples above.

pete
  • 24,141
  • 4
  • 37
  • 51