简体   繁体   English

将PHP与node.js / javascript连接

[英]Connecting PHP with node.js/javascript

I have to implement knapsack problem algorithm on my site that's based on PHP. 我必须在基于PHP的网站上实施背包问题算法。 I Thought that I will make it in JavaScript and I have ran into a problem. 我以为我会用JavaScript制作它,但遇到了一个问题。 I'm taking data that I need to take into this algorithm from MySQL, and JavaScript just can't do this. 我正在从MySQL获取需要纳入该算法的数据,而JavaScript不能做到这一点。 So I managed to do this by adding connector from Node.js. 因此,我设法通过从Node.js添加连接器来做到这一点。 The algorithm works when user is giving some input. 该算法在用户提供一些输入时起作用。 I made an form in HTML just to grab input and use it as parameter in my function. 我用HTML制作了一个表单,只是为了获取输入并将其用作函数中的参数。 The problem is now that whole website is wrote in PHP and I can't connect it with JavaScript, because of node.js part where I'm connecting to database. 现在的问题是,整个网站都是用PHP编写的,由于无法连接到数据库,因此我无法用JavaScript将其连接起来。 I want to ask if there is any way to use this. 我想问一下是否有任何方法可以使用它。

To sum it up and make it clear: website is in PHP, I want to make a simple HTML form to grab input from user into parameter function from JavaScript file. 概括起来并使其清晰:网站是PHP,我想制作一个简单的HTML表单以将用户的输入从JavaScript文件捕获到参数函数中。 Problem is that my JavaScript file needs MySQL data to run so it becomes node.js. 问题是我的JavaScript文件需要MySQL数据才能运行,因此它变成了node.js。 And from what I know it's not that easy to run node.js on PHP based webserver. 而且据我所知,在基于PHP的Web服务器上运行node.js并不是那么容易。 Is it possible to combine PHP with JavaScript so PHP will load data from MySQL into JavaScript? 是否可以将PHP与JavaScript结合使用,以便PHP将MySQL中的数据加载到JavaScript中? What you guys suggest? 你们有什么建议?

"I'm taking data that I need to take into this algorithm from MySQL, and JavaScript just can't do this". “我正在从MySQL获取需要纳入该算法的数据,而JavaScript不能做到这一点”。

An AJAX request from the JavaScript in your browser to a PHP script on your server is the straightforward solution to this. 从浏览器中的JavaScript到服务器上的PHP脚本的AJAX请求是解决此问题的简单方法。

To summarise the process: In the request, the JavaScript code sends some relevant parameters about what it wants, PHP receives that data, queries the database, and returns the results in the response to the AJAX request. 概括该过程:在请求中,JavaScript代码发送一些有关所需参数的参数,PHP接收该数据,查询数据库,并在对AJAX请求的响应中返回结果。 JavaScript receives this response, reads the data and continues its processing. JavaScript收到此响应,读取数据并继续处理。

Node.js is just another server-side scripting framework, analagous to PHP. Node.js只是另一个服务器端脚本框架,类似于PHP。 Don't let the fact it uses JavaScript as its programming language confuse you. 不要让它使用JavaScript作为其编程语言这一事实​​使您感到困惑。 There is absolutely no need to use Node in this scenario, if your existing server-side solution is written in PHP. 如果您现有的服务器端解决方案是用PHP编写的,则在这种情况下绝对不需要使用Node。

Browser-based JS and PHP can communicate with each other very happily using normal HTTP requests (likely initiated via AJAX, so you avoid page refreshes etc). 基于浏览器的JS和PHP可以使用正常的HTTP请求(可以通过AJAX发起,因此避免页面刷新等)非常愉快地相互通信。 That's the beauty of the web. 这就是网络的魅力。 The client and server can use totally different programming languages and different operating systems, and it makes no difference at all because they simply exchange data using a common protocol (HTTP) which both can understand. 客户端和服务器可以使用完全不同的编程语言和不同的操作系统,这完全没有区别,因为它们只是使用都可以理解的通用协议(HTTP)交换数据。

In your php file, inject your results, something like this. 在您的php文件中,注入结果,就像这样。

<?php
// function that handle form data and returns results from mysql
// let's call it my_data_function
$results = my_data_function();
?>


<script type="text/javascript">
  var mysql_data = <?php echo json_enode($results)?>
  my_js_function(mysql_data)
</script>

This is the old school way, then there was ajax, now we are talking REST API. 这是老派的方式,然后是ajax,现在我们谈论的是REST API。
Hope this helps. 希望这可以帮助。
Let me know if you need more help. 让我知道您是否需要更多帮助。

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

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