Get referral URL from form submission

Stu Furlong asked on March 29, 2018 23:52

Hey all,

I'm trying to see if it's possible to get a referral URL sent through a form.

We know you can get the referral URL using the macro: {% CurrentBrowser.UrlReferrer %}. One idea we had was to put this into the form as a hidden field and pass that value but what we get in the response is something like: % CurrentBrowser.UrlReferrer |(user)Role\testUser|(hash)4a221c5d124b140feaaad62727c3476498cd1768a7ee0ec87bd851aa367e0a38%}

Is it possible in Kentico to figure out the referral page when getting to the form, and passing that in the form response after submission?

Thanks!

Recent Answers


Peter Mogilnitski answered on March 30, 2018 01:31 (last edited on March 30, 2018 13:23)

your macro is not resolved correctly, but there is a tutorial: capture Referring URL on form postback

You may also try a quick way without going through all the complexity of creating form controls for hidden inputs and macros:

  1. Generate your form layout with submit button.
  2. Create new text field called 'referrer' and make it not required.
  3. Go back to form layout, switch to source and add:

    <script type="text/javascript">

var myinput = `$$input:referrer$$`;

myinput= myinput .replace(/text/g, "hidden");

myinput= myinput .replace(/<input/g, "<input value='" + document.referrer + "'");

document.write(myinput);

</script>

N.B. $$input:referrer$$ is wrapped in backticks to allow mulitline string in javascript

P.S. you should get something like this.

0 votesVote for this answer Mark as a Correct answer

Stu Furlong answered on March 30, 2018 16:54

Hey Peter,

I like where you're going with the script approach - the only issue I'm having is that the script gets ripped out of the form layout on the front end. I can save it ok and see it in the editor but on the front end the script is not on the page. Any ideas?

Thanks!

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 30, 2018 17:34 (last edited on March 30, 2018 17:45)

cant really say where the issue because I cannot see the html, javascript is not visible unless you have mistake somewhere. Is sounds like HTML formatting problem, not closed quote or something. I am not quite sure what means 'script gets ripped out' If your form is rendered as a table, you can put script inside the table:

<table class="form-table">
<tbody>
    <tr class="form-table-group">
        <td class="form-table-label-cell">$$label:Email$$</td>
        <td class="form-table-value-cell">$$input:Email$$</td>
        <td class="form-table-validation-cell">$$validation:Email$$</td>
    </tr>
    <tr class="form-table-group">
        <td class="form-table-label-cell">$$label:Message$$</td>
        <td class="form-table-value-cell">$$input:Message$$</td>
        <td class="form-table-validation-cell">$$validation:Message$$</td>
    </tr>
    <tr class="form-table-group">
        <td colspan=3>
        <script>
          var myinput = `$$input:referrer$$`;
          myinput= myinput .replace(/text/g, "hidden");
          myinput= myinput .replace(/<input/g, "<input value='" + document.referrer + "'");
          document.write(myinput); 
        </script>   
        </td>
    </tr>       
</tbody>

$$submitbutton$$

0 votesVote for this answer Mark as a Correct answer

Stu Furlong answered on March 30, 2018 17:48 (last edited on March 30, 2018 17:49)

I put the above code into the layout WYSIWYG HTML view and saved, but on the front end where the form is - everything on the form shows up except the script. The td colspan=3 is there - but it's just empty. Besides that though the form works as normal and submits ok. When I go back to the layout and view the HTML, I still see the script in there.

I was thinking that possibly CKEditor has a setting that is stripping out the script tag, but I don't think that's the case because the code is still in the WYSIWYG, just not appearing on the actual front end page.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 30, 2018 19:29 (last edited on March 30, 2018 19:41)

Ok. I would use a "on-line form" web part, add it to your page template, select your form in web part properties. Not quite sure about the CK editor here. How exactly do you add form?

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.