简体   繁体   English

将 JavaScript 变量传递给嵌入在 php 中的 html 时遇到问题

[英]Trouble passing JavaScript variables to html that is embedded in php

as my title indicates, I am having trouble passing a JavaScript variable to html that is embedded in PHP.正如我的标题所示,我无法将 JavaScript 变量传递给嵌入在 PHP 中的 html。 The PHP (html, JavaScript) file works and display's a document on the browser. PHP(html、JavaScript)文件运行并在浏览器上显示文档。 Where I need help is in calling a JavaScript function from the embedded html.我需要帮助的地方是从嵌入的 html 调用 JavaScript 函数。 As the document is being is being constructed, that's when the JavaScript function should be called so that the document will contain the retrieved JavaScript value.在构建文档时,应该调用 JavaScript 函数,以便文档将包含检索到的 JavaScript 值。 I have tried various solutions, but they do not work.我尝试了各种解决方案,但它们不起作用。 I hope my explanation is not too wordy.我希望我的解释不要太罗嗦。 If you need to see code, please let me know如果您需要查看代码,请告诉我

Unfortunately, I don't think it's going to work the way you hope.不幸的是,我认为它不会像您希望的那样工作。 The PHP is being run server side, before it is sent to the client. PHP 在发送到客户端之前正在服务器端运行。 The end user is downloading the constructed html before his or her browser ever starts running the javascript.最终用户在他或她的浏览器开始运行 javascript 之前下载构建的 html。

You are trying (by the sounds of it) to run PHP and Javascript at the same time to populate your tags... This isn't going to work.您正在尝试(通过它的声音)同时运行 PHP 和 Javascript 来填充您的标签......这将行不通。

Three Options:三个选项:

  • Do everything in PHP on the server and send the completed page.在服务器上用 PHP 执行所有操作并发送完成的页面。 (most logical) (最合乎逻辑)
  • Do what you can in PHP, send the page to the client and execute an AJAX call on page load to finish the job.在 PHP 中尽你所能,将页面发送到客户端并在页面加载时执行 AJAX 调用以完成工作。 (if javascript is needed) (如果需要javascript)
  • Use Node... (Javascript that runs on the server - it's a win / win situation)使用 Node ...(在服务器上运行的 Javascript - 这是一个双赢的局面)

Danny丹尼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM