来自 Javascript 的变量 -> php->聪明……可能吗?
示例:
<script type="text/javascript">
insertVideos({
'block':'youtubeDiv',
'q':'keyword',
'type':'search',
'results':8,
'order':'most_relevance',
'player':'embed',
'layout':'thumbnails'
});
</script>
我需要“结果”作为变量传递, 在过程结束时我需要类似的东西: {if $results != 0}
我该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有办法在 JavaScript 中访问 PHP/Smarty,因为它们在两个不同的世界中执行。
该页面是从服务器请求的。服务器执行 PHP 并使用 Smarty 生成内容。服务器将生成的 HTML 和 javascript 源代码发送到客户端。服务器现已完成。如果收到另一个请求,它将重新开始,而不知道前一个请求。
客户端接收 HTML 和 javascript 源并显示。
insertVideos
方法最终会被调用。此时,它与服务器和 PHP/Smarty 功能没有交互。从那里,javascript 可以向服务器发出 ajax 请求,或者如果我假设您只想在搜索找到一些视频(
结果 > 0
)时显示视频,您可以检查 javascript 并根据需要添加/修改 dom 元素。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.