简体   繁体   中英

Connecting PHP with node.js/javascript

I have to implement knapsack problem algorithm on my site that's based on PHP. I Thought that I will make it in JavaScript and I have ran into a problem. I'm taking data that I need to take into this algorithm from MySQL, and JavaScript just can't do this. So I managed to do this by adding connector from 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. 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. 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. Problem is that my JavaScript file needs MySQL data to run so it becomes node.js. And from what I know it's not that easy to run node.js on PHP based webserver. Is it possible to combine PHP with JavaScript so PHP will load data from MySQL into 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".

An AJAX request from the JavaScript in your browser to a PHP script on your server is the straightforward solution to this.

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 receives this response, reads the data and continues its processing.

Node.js is just another server-side scripting framework, analagous to PHP. Don't let the fact it uses JavaScript as its programming language confuse you. There is absolutely no need to use Node in this scenario, if your existing server-side solution is written in PHP.

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). 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.

In your php file, inject your results, something like this.

<?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.
Hope this helps.
Let me know if you need more help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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