Echo javascript variable into smarty

做~自己de王妃 提交于 2019-12-11 16:35:21

问题


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!!!


回答1:


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).




回答2:


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.



来源:https://stackoverflow.com/questions/4633172/echo-javascript-variable-into-smarty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!