简体   繁体   English

外部 JS 文件引擎——使用 node.js 操作数据库? PHP?

[英]External JS file engine — manipulating db using node.js? PHP?

Admittedly, I'm new to some of this...诚然,我是其中一些新手...

Building a website on a local server.在本地服务器上构建网站。 It has a ton of JS function in an external JS file.它在外部 JS 文件中有大量 JS 函数。

Site has a MYSQL DB.站点有一个MYSQL DB。 (I am still learning this). (我还在学习这个)。

As part of my calculations from functions in that external JS file, I want to update and/or read from that DB.作为我从该外部 JS 文件中的函数计算的一部分,我想更新和/或从该数据库读取。

I have been trying to read up on node.js and trying to read up on PHP (still learning both), but I'm not sure if I'm sniffing in the right direction.我一直在尝试阅读 node.js 并尝试阅读 PHP(仍在学习两者),但我不确定我是否在正确的方向上嗅探。

Do I somehow invoke functions from node.js from the external JS file?我是否以某种方式从外部 JS 文件中调用 node.js 中的函数? Do I somehow invoke the PHP (in the form of a function, I suppose) from the external JS file?我是否以某种方式从外部 JS 文件调用 PHP(我想以函数的形式)?

How does one typically do this?人们通常如何做到这一点?

I have definitely learned that this in the external JS file does not do the trick .我确实了解到,外部 JS 文件中的这行不通 First window appears, but second doesn't:第一个窗口出现,但第二个没有:

  //  Activate the node.js library for MYSQL access
alert("got here 1");
      var mysql = require('./mysql');
alert("got here 2"); // nope, this never pops up

Higher-level advice might be more useful than detailed in-the-weeds advice...?更高层次的建议可能比详细的杂草建议更有用......? Still very new to this.对此还是很陌生。

Thank you kindly!非常感谢你!

-=-=-=-=- -=-=-=-=-

self-muttering thoughts... I am using the external JS file to hold a bunch of functions that do all kinds of manipulation and conformation to the data that I collect on the front end:自言自语的想法......我正在使用外部JS文件来保存一堆函数,这些函数对我在前端收集的数据进行各种操作和构造:

<button class="ButtonOperation" onclick="DataLog(document.getElementById('DataWindow').value,'NE_AssembleOrder')">Log Data</button>

Am I eventually going to discover that I should instead port all of these functions over to a big PHP file instead?我最终会发现我应该将所有这些函数移植到一个大的 PHP 文件中吗?

-=-=-=-=- -=-=-=-=-

Okay, took a while before I better understood this.好的,我花了一段时间才更好地理解这一点。 So, this is the answer that would have gotten me moving in the right direction (for any future reference):所以,这是让我朝着正确方向前进的答案(供将来参考):

The thing to understand is that for this project, you want to manipulate data to and from a database, which means that (at least for now, for the sake of simplicity), the key is to get your data into a package and send it up to the server, and then have a function running on the server take up the yoke from there.需要理解的是,对于这个项目,您希望在数据库之间操作数据,这意味着(至少现在,为了简单起见),关键是将您的数据放入一个包中并发送到服务器,然后在服务器上运行一个函数,从那里接管轭。

The way to do that (old school), is with a form.这样做的方法(老派)是使用表格。

When you SUBMIT a form, all that data on the form is bundled up and sent to the server.当您提交表单时,表单上的所有数据都被捆绑并发送到服务器。

In this instance you have an index.html page, and that page will open a new page for each of the functions you're trying to track.在本例中,您有一个 index.html 页面,该页面将为您尝试跟踪的每个功能打开一个新页面。 Use JavaScript to pop open the window and then when you include the URL for the window, pop in a Popup_ SpecificFunction .php file.使用JavaScript弹出打开窗口,然后当你包括在窗口中的网址,弹出一个Popup_ SpecificFunction PHP文件。 (change SpecificFunction as needed) (根据需要更改特定功能

So far, so good.到现在为止还挺好。 ;) ;)

Now, in that Popup_ SpecificFunction .php, you will collect all your data under a single form .现在,在Popup_ SpecificFunction .PHP,你会收集单一形式下的所有数据。 A good ol' HTML form, with a [SUBMIT] button.一个很好的 HTML 表单,带有 [提交] 按钮。 That very same Popup_ SpecificFunction .php file also has a reference in the header, referring to the big main library of PHP functions -- which is a file sitting on the server.这非常相同Popup_ SpecificFunction PHP文件也有头部的引用,指的是PHP函数的大主库-这是坐在服务器上的文件。

That [SUBMIT] button invokes a ProcessAllThisData function -- which is on the server-side PHP file.该 [SUBMIT] 按钮调用ProcessAllThisData函数——该函数位于服务器端 PHP 文件中。 In doing this, it sends all the data from the form -- including a lot of data you include in hidden controls -- to the serverside function.在执行此操作时,它将表单中的所有数据——包括您包含在隐藏控件中的大量数据——发送到服务器端函数。

And at that point, you are basically "on the server" with all your data, and then you can just code that function in PHP and manipulate the database and other stuff as needed.在这一点上,您基本上“在服务器上”拥有所有数据,然后您可以只用 PHP 编写该函数并根据需要操作数据库和其他内容。

Using the form was the thought-jump you needed, because prior to this, you've generally thought of forms as standalone data, but they can have actions associated with the entire forms.使用表单是您需要的思维跳跃,因为在此之前,您通常将表单视为独立数据,但它们可以具有与整个表单相关联的操作。

You can still use JavaScript to do client-side stuff, but here's another thing that can trip a person up:你仍然可以使用 JavaScript 来做客户端的事情,但还有一件事情会让人绊倒:

There is a difference between these two HTML items as far as whether or not you should use them to send data to and from the server, or whether or not you are just going to JavaScript something on that button:这两个 HTML 项目之间的区别在于您是否应该使用它们向服务器发送数据和从服务器发送数据,或者您是否只是在该按钮上使用 JavaScript 某些内容:

<button></button>

and

<input type="button"></input>

You might have to experiment a bit to figure out which is which.您可能需要进行一些实验才能确定哪个是哪个。

This was everything you needed to get you moving in the right direction.这是让您朝着正确方向前进所需的一切。

Sincerely,真挚地,

Future Me.未来的我。 :) :)

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

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