1

I believe I have most of this right, but may be messing up the order, and can't get a fiddle to save for some reason. Basically I need to grab part of a form Input and append it as a query string on submit so as to trigger the affiliate cookie on the next page. All codes would start with a AAA-11 value and then the actual affiliate reference code. So

<form>
    <label>Affiliate Code</label>   
    <input type="text" id="code">
    <input type="submit" id="submit_btn">        
</form>

And the JS:

$('#submit_btn').on('click', function() {
    var str = $('#code').val();
    var acode = str.substr(6);
    location.href = location.href + "?ref=" + acode;
});

What am I missing here?

1 Answer 1

1

Your button is submitting the form because of its attribute type="submit"

You can change the attribute to type="button", or programmatically prevent form submission using event.preventDefault(), so that you can modify the data.

$('#submit_btn').on('click', function(event) {
    event.preventDefault();
    var str = $('#code').val();
    var acode = str.substr(6);
    location.href = location.href + "?ref=" + acode;
});

[EDIT] (comment)

If you need to modify the form action and submit the form aswell, use this:

$('#submit_btn').on('click', function(event) {
    event.preventDefault();
    var str = $('#code').val();
    var acode = str.substr(6);
    // change form action attribute and submit the form:
    $(this).closest('form').attr('action', location.href + "?ref=" + acode).submit();
});
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks, makes sense! But how can I still have it submit a few seconds after? Or alternatively, include the query string live as soon as the input is filled in by the user?
I'm not sure if I well understand you. Maybe something like: if( !$('#code').val() ){ event.preventDefault() } ?
I mean that preventDefault() will stop the submit from happening.. but I still need the form to submit.
Then you can put the preventDefault() in a simple if statement to determine whether the form should be submitted or not, just like in my previous comment. Of course the if condition you set is up to your needs... BTW. The input field has no name, so it won't be submitted anyway. Also, you can use cookies (if you need them on current page) directly in javascript. As for the JSFiddle - you can't save it when the javascript section includes location. code.
I understand the conditional part, but maybe then preventDefault is not the best way to do this. I want it work like so: User is on form at mysite.com/stuff, fills out affiliate code among other values, clicks submit > Query string parameter is attached so the url is now mysite.com/stuff?ref=code, then form is submitted and the parameter passed on to the confirmation page where the affiliate software pixel or script will read it as if someone had come form a standard affiliate link (the affiliate system is a third party software). Sorry If I was no clear about it earlier.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.