how to assign a javascript variable to a smarty variable

后端 未结 2 1365
Happy的楠姐
Happy的楠姐 2020-12-22 09:07

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


        
相关标签:
2条回答
  • 2020-12-22 09:22

    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.

    0 讨论(0)
  • 2020-12-22 09:39

    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

    0 讨论(0)
提交回复
热议问题