Variable from Javascript -> Php -> Smarty… is it possible?

天大地大妈咪最大 提交于 2019-12-25 06:26:29

问题


Example:

<script type="text/javascript">
    insertVideos({
        'block':'youtubeDiv',
        'q':'keyword',
        'type':'search',
        'results':8,
        'order':'most_relevance',
        'player':'embed',
        'layout':'thumbnails'
    });
</script>

i need that 'results' as a variable to pass, at the end of process i need something like: {if $results != 0} how can i do it?


回答1:


There isn't a way to access the PHP/Smarty in your javascript as they are executed in two different worlds.

The page is requested from the server. The server executes the PHP and generates the content with Smarty. The server sends the resulting HTML and javascript source code to the client. The server is now done. If another request is received, it starts over anew without knowledge of the previous request.

The client receives HTML and javascript source and displays. The insertVideos method eventually gets calls. At this point, it has no interaction with the server and the PHP/Smarty features.

From there, the javascript could make an ajax request back to the server or if I make an assumption that you only want to display the videos if the search finds some (results > 0), you can make that check in javascript and add/modify the dom elements as needed.



来源:https://stackoverflow.com/questions/6672929/variable-from-javascript-php-smarty-is-it-possible

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