Ad

Set Form Value From Url Parameter

- 1 answer

I am using Typo3 10LTS and I would like to set the value of a form field with a parameter value from the URL.

E.g. the value "123" should be entered into the form field "field-1" via the URL www.test-xyz.de/sidename?field-1=123.

Thanks, Stephan

Ad

Answer

Fluid

Let's say we have a fluid link, with parameter "123"

<f:link.page pageUid="999" additionalParams="{param: '123'}" noCacheHash="true">Link to Form</f:link.page>

This sends the parameter to the page with the form, containing the value in the URL:

www.test-xyz.de/sitename?param=123

See: link.page Documentation

jQuery

We can now use a few lines of jQuery to populate a field with the value from the URL:

$(".myForm").each(function() {

    // Function to sanitize output
    
    function getURLParameter(name) {
      return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
    };
    
    // Populate the input field '.field1' with the parameter in the URL

    if (getURLParameter('param') !== null) {
        var value = getURLParameter('param');
        $('.field1').each(function(){ 
            $(this).val(value); 
        }); 
    };

}); 
Ad
source: stackoverflow.com
Ad