This forum is no longer open and is for reading/searching only.

Please use our new MachForm Community Forum instead.

MachForm Community Forums » MachForm 4

Prefill form fields from URL variable pairs


  1. businessschool
    Member

    I'm having problems preloading my machform fields with data passed in via the URL. My form is included using the Javascript code into a Drupal website/page. The preloading works when I do it via the View function in the MachForm builder and load the URL with variable pairs, BUT as soon as I embed the form in my Drupal site, the preload fails. This is a very important function for our business, has anyone had any problems with this functionality before? Does the URL have to reference the form id when it is embedded in a webpage?

    I have tried the following to no avail

    [myformURL]?element1=testing&element2=testingagain
    [myformURL]?form_id=13941&element1=testing&element2=testingagain
    [myformURL]?formid=13941&element1=testing&element2=testingagain

    Any thoughts/assistance greatly appreciated

    Posted 8 years ago #
  2. Saghalie
    Pro Member

    What embed method are you using?

    Saghalie
    http://www.kcmhosting.com

    Posted 8 years ago #
  3. businessschool
    Member

    Hey Saghalie, thanks for getting back to me... can't find a setting on this forum to notify me when replies are posted :=].

    I am using the Javascript embed method...

    I am loading my URL with variables that are parsed from contact records in my CRM. I have tried manually loading the variables in the URL, doesn't work in a form that is embedded in a Drupal site, does work in the View option from the MachForm interface e.g. this works from the View menu

    [mywebsiteURL]/view.php?id=14337&element_5=Whatever

    This DOES NOT work in my drupal site with an embedded form

    [mywebsiteURLwithformembedded]?element_5=Whatever&element_6=Whatelse

    I have checked the names of the form fields and they are as above, element_[n]

    Thanks for any assistance you can give me

    Marty

    Posted 8 years ago #
  4. frankzobitz
    Member

    Are you modifying the URL of the code within the javascript?

    The second line of the javascript code is something like this: (I just took from the demo site)
    var __machform_url = 'http://www.appnitro.com/demo/embed.php?id=6';

    That is where you want to append your: &element_5=Whatever&element_6=Whatelse
    so this would be the code you would paste onto your website:

    <script type="text/javascript">
    var __machform_url = 'http://www.appnitro.com/demo/embed.php?id=6&element_5=Whatever&element_6=Whatelse';
    var __machform_height = 627;
    </script>
    <div id="mf_placeholder"></div>
    <script type="text/javascript" src="http://www.appnitro.com/demo/js/jquery.min.js"></script>
    <script type="text/javascript" src="http://www.appnitro.com/demo/js/jquery.ba-postmessage.min.js"></script>
    <script type="text/javascript" src="http://www.appnitro.com/demo/js/machform_loader.js"></script>

    Posted 8 years ago #
  5. businessschool
    Member

    Hey Frankzobitz, thanks for taking the time to reply.

    I need to fill the elements with values that are present in the URL, e.g. if the URL was

    http://whatevermysiteis.com/index.php?name=whatever&gender=male

    I would like for example to have element_3 have the value of the name variable i.e. whatever and element_4 to have the value of hte gender variable i.e. male

    I am currently trying to implement a custom JS that fires on page load, grabs all the variables from the URL and sets the values of the form elements as defined in the script, not much success so far :=] Any help appreciated, current script is as follows:

    <script type="text/javascript">

    function getParameterByName(name)
    {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.search);
    if(results == null)
    return "";
    else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    // Load all form elements with variable values here
    window.onLoad = function(){
    $("#element_5").val(getParameterByName('last_name'));
    }
    </script>

    Cheers

    Posted 8 years ago #
  6. businessschool
    Member

    OK, so found something interestingish, DON'T include the <script type="text/javascript"> tags in your custom javascript, I have added a couple of alerts to check that the script is loading and it appears to be.. must have errors in my code for it not to work

    function getParameterByName(name)
    {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.search);
    if(results == null)
    return "";
    else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    // Load all form elements with variable values here
    window.onload = function(){
    window.alert("Onload function") ;
    $("#element_3").val(getParameterByName('first_name'));
    $("#element_4").val(getParameterByName('last_name'));
    }
    window.alert("In the script") ;

    Posted 8 years ago #

RSS feed for this topic

Reply