2

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

Example:

{literal}
<script type="text/javascript">
  var js_variable = 110;
</script>
{/literal}

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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}");

5 Comments

Thank you for your prompt response, I tried using this method but always i get a null object :(
Could you give more details about what you are trying to do? Client-side scripting (Javascript) really doesn't look right here, considering Smarty is a PHP templating engine. Have you considered using Smarty variables?
Yes of course, Im Trying to set and dynamic var in smarty using Javascript: 100013 = 'Hy my number is %1$d' jQuery('div.fakbox_msg').html("{/literal}{lang_sprintf id=100013 1=" + js_variable + "}{literal}"); The code is Correct because when i try to remove the {literal} tag the js_variable works: Example: {literal}<script type="text/javascript">var js_variable = 110;</script>{/literal} jQuery('div.fakbox_msg').html("{lang_sprintf id=100013 1=" + js_variable + "}"); The Output is: {lang_sprintf id=100013 1=110}
And using {literal}{/literal} tag: {literal}<script type="text/javascript">var js_variable = 110;</script>{/literal} jQuery('div.fakbox_msg').html("{/literal}{lang_sprintf id=100013 1=" + js_variable + "}{literal}"); The Output is: Hy my number is 0 But neither is correct I need the output to be Hy my number is 110
This is not possible. PHP is executed on the server side, and JavaScript on the client side. Once the JavaScript gets executed, there is no concept of Smarty anymore.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.