如何在加载时在 php 中发布值(无需单击任何内容)
我想将一个变量从我的 php 页面发送到我的 jquery 代码。我知道我可以使用 $_post 和 $_get 来做到这一点,但是据我所知,它们涉及用户的操作,以触发“发送”操作。 我正在寻找在页面加载时发送此信息(因此用户不必(必须)知道正在发生的情况)
我找到了此信息,但它可能脱离上下文或者可能不是我正在寻找的内容,但以下内容不起作用:
$.post("demo.php", { name: "John", time: "2pm" } );
为了让您知道我为什么问,我有一个图像数组,以及该数组所具有的图像数量的计算。我需要将此值植入 jquery,以便我可以将它用于各种宽度等等...我唯一想到的是将 html 上的值作为隐藏元素,但这是不对的在可访问性和谷歌搜索方面。
I want to send a variable from my php page to my jquery code. I know that I can do this using $_post and $_get, however as far as I know, they involve an action from the user, to trigger the "send" action.
I am looking for away to send this as the page loads (so the use does not (have to) know that that is going on)
I found this, but it might be out of context or might not be what I am looking for, but the following does not work:
$.post("demo.php", { name: "John", time: "2pm" } );
Just so that you know why I am asking, I have an array of images, and a calculation of the number of images the array has. I need to seed this value to jquery so that I can use it for various widths and so on... The only what that I thought of doing this was to have the value on the html as a hidden element, but that is not right in terms of accessibility and google search.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来你试图从服务器端(php)获取一些变量到客户端(javascript)。一种解决方案是在服务器端代码中生成 javascript 变量。
例如,
这只是一个演示。您应该了解这是如何工作的。
It seems you trying to get some variables from the server side (php) to the client side (javascript). One solution would be to generate the javascript variables in your server side code.
e.g.
This is just a demo. You should get the idea how this works.