简体   繁体   English

如何将动态 HTML 连接到 MySQL

[英]How to connect dynamic HTML to MySQL

First, I'd like to say this is part of a university undergrad project, so my knowledge will be limited.首先,我想说这是大学本科项目的一部分,所以我的知识会受到限制。

I have a MySQL database set up with data and I want to click a button in HTML that queries the database and updates HTML table elements with returned data.我有一个用数据设置的 MySQL 数据库,我想单击 HTML 中的一个按钮,该按钮查询数据库并使用返回的数据更新 HTML 表格元素。 I have done A LOT of reading and it always returns to nodejs which I have absolute beginner experience with.我做了很多阅读,它总是返回到我有绝对初学者经验的 nodejs。 I have code set up to query my database with nodeJS which works fine, however, I see absolutely no solution to update the HTML dynamically and I refuse to believe it is not in some way possible.我设置了代码来使用 nodeJS 查询我的数据库,它工作正常,但是,我完全没有看到动态更新 HTML 的解决方案,我拒绝相信它在某种程度上是不可能的。 I have read about expressJS templating but this dynamic updating of data is expected to occur hourly from the database (the database is a dummy stand-in for a real-life one) and the expressJS templates are not a good fit at all.我已经阅读了有关 expressJS 模板的信息,但是这种数据的动态更新预计会每小时从数据库中进行一次(数据库是现实生活中的虚拟替代品),并且 expressJS 模板根本不适合。

How can I return nodeJS data to a HTML page without serving a new page each time?如何在不每次都提供新页面的情况下将 nodeJS 数据返回到 HTML 页面? perhaps return server-side JS data to client-side JS?也许将服务器端 JS 数据返回给客户端 JS? how does nodeJS allow the HTML to then load client-side JS files? nodeJS 如何允许 HTML 然后加载客户端 JS 文件? I am unable to get a PHP install working so it's unfortunately not an option.我无法让 PHP 安装工作,所以很遗憾这不是一个选项。 I am open to any alternatives that allows HTML to MySQL connections.我对任何允许 HTML 到 MySQL 连接的替代方案持开放态度。

Thanks谢谢

I'm not 100% sure what you have set up or what your level of knowledge is, but I hope I can help.我不是 100% 确定你设置了什么或你的知识水平是什么,但我希望我能提供帮助。

Sounds like you have your server running and querying your DB just fine, so that's good.听起来您的服务器正在运行并查询您的数据库就好了,这很好。 Now you need to route that to your frontend.现在您需要将其路由到您的前端。 This is where expressJS usually comes in. It's a backend framework for creating API's in node.这就是 expressJS 通常出现的地方。它是用于在节点中创建 API 的后端框架。 Think of it as a middleman.把它想象成一个中间人。 You send a request to your API, and your API makes a request to the DB and gives you what you want.您向您的 API 发送请求,您的 API 向数据库发出请求并为您提供所需的内容。

To make the request from the frontend, you can use the fetch() API.要从前端发出请求,您可以使用 fetch() API。 It's an asynchronous function used to make https requests and receive data.它是一个异步函数,用于发出 https 请求和接收数据。 You then need to use that data to dynamically populate your html.然后,您需要使用该数据来动态填充您的 html。 Since it's done with javascript, it won't force a reload of the page either.由于它是使用 javascript 完成的,因此它也不会强制重新加载页面。

Feel free to ask questions and hopefully we can get you pointed in the right direction at the very least.随时提出问题,希望我们至少可以让您指出正确的方向。

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

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