Unable to display multiple parameters from url by javascript through shortcodes

From our CRM I can generate URL's for customers that includes parameters. I want to use the information stored in this parameters to show dynamic content to the customers.

Example: www.example.com/page/?firstname=Johnlastname=Doeage=22

I need to display this information as the following:

Firstname: John
Lastname: Doe
Age: 22

For this I used the following code in my functions.php

?php

function dynamicParameters($atts){
    //Allow to call a parameter within a shortcode
    $a = shortcode_atts( array(
        'dynamic' = ''), $atts);
        
    //Store the parameter in variable
    $content = $atts['dynamic'];

    //Create a span and add a piece of javascript that fills the span with the right parameter
    $result = 'span id=dynamic' . $content . '' . $content . '/spanscript const currentURL = window.location.search; const url = new URLSearchParams(currentURL); let ' . $content . ' = url.get(' . $content . '); document.getElementById(dynamic' . $content . ').innerHTML = ' . $content . ';/script';

    return $result;
}
//Add shortcode so I can use a shortcode like [DynamicContent dynamic=exampleparameter]
add_shortcode('DynamicContent', 'dynamicParameters');

By this I can just type the following input in my Wysiwyg-editor:

Firstname: [DynamicContent dynamic=firstname]
Lastname: [DynamicContent dynamic=lastname]
Age:  [DynamicContent dynamic=age]

The script writes it all as it should be, but it will only work the first time a shortcode is used. In this case the output is

Firstname: John
Lastname: lastname
Age: age

At least it transforms the shortcodes to a certain value, but it seems like the javascript can just used one time, even though the shortcodes generates a new script for every parameter I use in a shortcode.

Does anyone know what I do wrong?

Topic variables php urls Wordpress parameter javascript

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.