开发者

Passing JavaScript variable to smarty

开发者 https://www.devze.com 2023-03-23 04:43 出处:网络
I\'m having some trouble passing a variable from JavaScript to smarty. Example: {literal开发者_如何学C}

I'm having some trouble passing a variable from JavaScript to smarty.

Example:

{literal开发者_如何学C}
<script type="text/javascript">
  var js_variable = 110;
</script>
{/literal}

jQuery('div.fakbox_msg').html("{/literal}{lang_sprintf id=100013 1=js_variable}{literal}");


You cannot do that in an easy way. PHP, and by extension Smarty, is parsed and run on the server side before the browser get the data. JavaScript is run on the client side when the browser parses the HTML, CSS and Javascript that the server sent.

You will have to make a new HTTP request in some way, sending the new data. You can do that by reloading the entire web page and sending stuff in the querystring (after ? in the URL), or slightly more advanced by doing an Ajax call from your JS code and make JS do the changes of the page that you desire. The latter is more complex and requires some knowledge of Javascript, but the page does not need to be reloaded in its entirety. for more info : Assign JavaScript variable to Smarty variable


I never used Smarty, so I may be wrong, but from what I see on your code. this should work:

jQuery('div.fakbox_msg').html("{/literal}{lang_sprintf id=100013 1=" + js_variable + "}{literal}");


Smarty cannot use client side variables, such as the ones created by JavaScript.

0

精彩评论

暂无评论...
验证码 换一张
取 消