douhuang5331
2011-08-17 15:40
浏览 44
已采纳

如何在没有AJAX,Jscript,HTML隐藏字段或cookie的情况下从JavaScript变量获取值到PHP变量?

How to get a value from a JavaScript variable to a PHP variable without AJAX, Jscript, HTML hidden field, or a cookie ?

(from PHP to JavaScript: var javaScriptVar = "<?php echo $someVar; ?>"; ) So is there nothing like that?

thanks

图片转代码服务由CSDN问答提供 功能建议

如何在没有AJAX,Jscript,HTML隐藏字段或a的情况下从JavaScript变量获取值到PHP变量 cookie?

(从PHP到JavaScript: var javaScriptVar =“&lt;?php echo $ someVar;?&gt;”; ) 没有什么比得上 那个?

谢谢

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • duanji2002 2011-08-17 15:43
    已采纳

    You can do it like in your example, but your javascript must either be inside a script tag which is in a PHP file, or in an external 'js' file that you save with a php extension.

    Or, you can set your server up so that all files with a 'js' extension also get parsed by PHP, but that's probably more than overkill.

    However, since the PHP will only be executed once before the file is returned to the client, there's no way to make that dynamic. If your objective is to do something simple with data that resides on the server (and more than likely will not change between page accesses) it won't be too horrible to accomplish what you want to do that way.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duannv2081 2011-08-17 15:43

    There is no way to get a Javascript value from PHP, because the response line goes from server to browser, never the other way around. AJAX is your best bet in this case.

    评论
    解决 无用
    打赏 举报
  • dongping1689 2011-08-17 15:43

    There is nothing like that. The data needs to be transfered to the server, either by a form variable or a url variable. Either with an AJAX request or a normal request. I never thought about doing it with a cookie, but I guess that is an option too.

    评论
    解决 无用
    打赏 举报
  • doukanhua0752 2011-08-17 15:44

    No.

    PHP is a server sided language, javascript is a client sided language.

    In order for PHP to receive information, you must talk to the server. The only way to do so is to submit a form, or use AJAX.

    评论
    解决 无用
    打赏 举报
  • dongza6247 2011-08-17 15:45

    Short answer is you can't. There are two main ways to send content to the server from the client, which are cookies and submitted data (either posted or through the querystring). If you were so inclined you could add it to a request header but that is plain obscure and would require an AJAX request.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题