user567792
user567792

Reputation: 1

Echo javascript variable into smarty

I set one variable in java-script. And I want to echo that variable in smarty.

Would it be possible?

If yes then please provide me some guidance.

Cheers!!!

Upvotes: 0

Views: 963

Answers (2)

Quentin
Quentin

Reputation: 943586

Generally speaking (i.e. unless you are doing something esoteric such as mixing SSJS and PHP) you can't.

The PHP runs (processing the Smarty template), then delivers the output to the browser, and then the JavaScript runs, but which time it is too late to get any data back.

You could use JavaScript to work out what data you want, and then make a new request to the server. e.g. by setting location to a new URI or by using XMLHttpRequest to get new data from the server and then use that to modify the document with JS. However, since you are looking at the Smarty level, it is likely that you only want to the data for formatting, in which case you should just do it in JS (with DOM manipulation).

Upvotes: 2

Matthew Flaschen
Matthew Flaschen

Reputation: 284836

You can use an AJAX request to send it to the server, where you can use Smarty to render new HTML (don't know the details of this), then return HTML in the AJAX response. You can then inject this with innerHTML or similar.

You can look at the XMLHttpRequest object. However, various JavaScript frameworks can greatly simplify this.

Upvotes: 1

Related Questions