Skip to content

How to use a js twig variable from my php controller? #42304

Closed
@crimanlor

Description

@crimanlor

I´ve created a twig with autocomplete google places API input and functionality. Now I need use the place variable with the data received in my php controller, but I don´t know how I do this. Somebody could help me? Thanks!

{% form_theme form 'form/form_theme.html.twig' %}
{% block address_form_widget %}`

<script>
let autocomplete;
function initAutocomplete()
{
autocomplete = new google.maps.places.Autocomplete(
document.getElementById('autocomplete'),
{
types: ['establishment'],
componentRestrictions: {'country' : ['ES']},
fields: ['place_id', 'geometry', 'name']
}
);

        autocomplete.addListener('place_changed', onPlaceChanged);
    }

    function onPlaceChanged()
    {
        var place = autocomplete.getPlace();

        if(!place.geometry)
        {
            document.getElementById('autocomplete').placeholder = 'Enter a place';
        } else {
            place = place.name . place.geometry;
            document.getElementById('details').innerHTML = place;
        }
    }
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=myApiKeyHere&libraries=places&callback=initAutocomplete" async defer></script>

{% endblock %}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions