roxitsc of Instapage Tech Support
11/3/2015 - 1:13 AM

Prefill input fields with URL parameters

Prefill input fields with URL parameters

<script>
// version 2 - check if parameters exist in the URL, if they don't leave input field in default state
  var getUrlVar = function getUrlVar(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};
 	// Prefill input fields with URL parameters
  	ijQuery(document).ready(function($){ 
        setTimeout( function() {
          	if (getUrlVar("firstname") != null) {
              var fname = ijQuery( '.email-form' ).find( 'input[name="'+ base64_encode( 'First Name' ) +'"]').val("[firstname]");
            }
          if (getUrlVar("lastname") != null) {
              var lname = ijQuery( '.email-form' ).find( 'input[name="'+ base64_encode( 'Last Name' ) +'"]').val("[lastname]");
          }
          if (getUrlVar("email") != null) {
              var email = ijQuery( '.email-form' ).find( 'input[name="'+ base64_encode( 'Email *' ) +'"]').val("[email]");
          }
          if (getUrlVar("phone") != null) {
              var phone = ijQuery( '.email-form' ).find( 'input[name="'+ base64_encode( 'Mobile Phone *' ) +'"]').val("[phone]");
          }
          if (getUrlVar("suburb") != null) {
               var sub = ijQuery( '.email-form' ).find( 'input[name="'+ base64_encode( 'Suburb' ) +'"]').val("[suburb]");
          }
          }, 1500 );
  	});
</script>
<script>
 	// Prefill input fields with URL parameters
  	ijQuery(document).ready(function(){ 
        setTimeout( function() {
              var fname = ijQuery( '.email-form' ).find( 'input[name="'+ base64_encode( 'First Name' ) +'"]').val("[firstname]");
              var lname = ijQuery( '.email-form' ).find( 'input[name="'+ base64_encode( 'Last Name' ) +'"]').val("[lastname]");
              var email = ijQuery( '.email-form' ).find( 'input[name="'+ base64_encode( 'Email *' ) +'"]').val("[email]");
              var phone = ijQuery( '.email-form' ).find( 'input[name="'+ base64_encode( 'Mobile Phone *' ) +'"]').val("[phone]");
               var sub = ijQuery( '.email-form' ).find( 'input[name="'+ base64_encode( 'Suburb' ) +'"]').val("[suburb]");
          }, 1500 );
  	});
</script>