how to assign a javascript variable to a smarty variable

你离开我真会死。 提交于 2019-12-18 09:45:43

问题


I got a problem regarding how to assign a java script variable to a smarty variable. Here is the code snippet.

function getDateInfo(date, wantsClassName)
{                  
    var as_number = Calendar.dateToInt(date); //This as_number is the variable which should be assigned to smarty variable    
}

How can i accomplish this.

any help will be appreciated..

Thanks n advance -- Fero


回答1:


You can't assign a client-side value to a smarty variable, as smarty is a templating language that runs on the server. Smarty assignments can only be done on the server-side, that is to say, from PHP. E.g.:

$smarty->assign('timestamp',time());

So what you can do is something like:

    $smarty->assign('timestamp',time()); //in your PHP script

    //in your JS
    var currentTS = {$timestamp};

See http://www.smarty.net/manual/en/api.assign.php




回答2:


For anything AJAX with PHP, I'd propose the xajax library:

http://www.xajax-project.org/

Using xajax, you register a PHP function to be exposed to your client side JS code. By magic, if you call registered functions in JS, xajax packs the request with parameters and passes it back to the server.

Moreover, you are able to push more than update from your PHP code to the browser. Something like 'Replace element X with ... and replace element Y with ...' in a single XHR reply.

xajax makes AJAX style code very clean.



来源:https://stackoverflow.com/questions/1271026/how-to-assign-a-javascript-variable-to-a-smarty-variable

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