SmartPORTAL: API and example populating the form using URL parameters

SmartPORTAL: API and example populating the form using URL parameters

SmartPORTAL has an API which allows you to hook into events.  This allows you to do a lot of different things as you can alter the form or form fields prior to the display of the form to the end user. 

The JavaScript function hook_profile_form_ready(profile) is called when the form is ready, If profile is null the user is not currently logged in, if they are logged in the profile will contain a full user record for them including email, name, address, phone etc.

Example: Populating a form using URL parameters

Scenario: You may have a signup form on your website home page which just asks for email address. Once completed the user clicks next and is taken to a more complete update form with their provided email address already added to that form. The link to the form could be something like:
https://[domain]/what-we-do/keeping-you-date/sign-up-for-updates?email=warren%40smartthing.org 

On the SmartPROFILE form page you could add the below script, this auto-populates the email address field in the form.

  1. <script>
    function getURIParameter(param, asArray) {
        return document.location.search.substring(1).split('&').reduce(function(p,c) {
            var parts = c.split('=', 2).map(function(param) { return decodeURIComponent(param); });
            if(parts.length == 0 || parts[0] != param) return (p instanceof Array) && !asArray ? null : p;
            return asArray ? p.concat(parts.concat(true)[1]) : parts.concat(true)[1];
        }, []);
    }

    function hook_profile_form_ready(profile) {
        var email =  getURIParameter('email');
        if (email) {
              $('[id="sp-email|address"]').val(email).change();
        }
    }
    </script>