There seems to be a fundamental misunderstanding here. Twig code runs on the server, before the JS gets anywhere near being executed. Once the Twig code has been executed, then the resulting page goes to the browser and the JS is executed.
In effect, this question is "how can my code travel back in time"?
I recommend using the Javascript Routing system (https://symfony.com/doc/current/routing/generate_url_javascript.html) to generate the url you need in Javascript directly. This looks something like
// JAVASCRIPT
var url = Routing.generate('email_index', {
'iduser': iduser,
'msg': saisie
});
Okay, you've asked a follow-up question about how this should all be put together. I'd recommend not using the onclick
attribute of the button element: it may be what you've done in class but it's considered outdated. Get a reference to your button:
var myButton = document.querySelector("button");
Pass the iduser
value into Javascript (one of the two options):
var iduser = {{ onlineorder.iduser.iduser }}; // if it's an int
var iduser = "{{ onlineorder.iduser.iduser | e('js') }}"; // if it's a string.
// the e('js') is a Twig function to safely generate Javascript strings, see
// https://twig.symfony.com/doc/2.x/filters/escape.html
Then use the first code block above to generate url
.
Finally set the event handler on the button: (docs)
button.addEventListener("click", function(event) {
window.location.href = url;
});
If you have any further issues I'd recommend asking your lecturer/supervisor.