0

I am trying to pull a URL query string variable into a hidden form field to submit with an email opt-in.

The URL looks like this: http://domain.com/page/?tid=123456

I am able to pull the tid query string as I require with this code:

<script>
function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, null])[1] || ''
    );
}

But I don't know how to place getURLParameter('tid') in the hidden field.

I have tried inline script but it looks like this doesn't work with Javascript.

<div id="optin">
<form id="em-optin" class="moonray-form-clearfix" action="https://forms.moon-ray.com/v2.4/form_processor.php?" onsubmit="return validateForm();" method="post" accept-charset="UTF-8" _lpchecked="1">

    <div style="display: none;">
        <input name="subid_198" class="moonray-form-input" type="hidden" id="mr-field-element-1" value="<script>getURLParameter('tid')</script>">

    <input name="email" type="text" class="email" value="Enter Your Email:" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;" tabindex="501">
    <input type="submit" name="submit-button" value="Sign-up Now!" class="submit" id="mr-field-element-4" tabindex="502" "=" ">
</form>
</div>

Is anyone able to advise how I do this?

1 Answer 1

1

Hipporter. Have you tried to assign the value by using the following code:

var your_var = getURLParameter(name);
document.getElementById('mr-field-element-1').value = your_var;
Sign up to request clarification or add additional context in comments.

1 Comment

Yes! That has nearly done it. :) Slight alteration was: getURLParameter('name') but I placed all that after the query string pull and dropped it after the div. Seems to have worked. Thanks!

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.