How to use Javascript to manipulate data from a database?

Asked

Viewed 902 times

5

I am now beginning to deal with web development in PHP. We know that to have an efficient and dynamic system, we recommend using Javascript, such as, Jquery, Angularjs, Backbone.js and others. But from what I know of Javascipt, it does not have an API to connect to the database.

I have a system to develop in PHP to manipulate data from the database. In this case I need to "take" the data in the database and generate a chart, using for example the Google Charts API. The first thing I thought, is to manipulate Javascript code with PHP, ie, put the tags Javascript inside the PHP code (code to connect to the database) and get the data and insert into Javascript. However, this would not be a good programming practice, because my code would be unreadable to manipulate and I would have future problems with my system.

I would like to get experienced programmers on how to get around this situation. Basically, how can I manipulate data from the database in Javascript, Angularjs, Google Charts and etc. and how can I get a good integration between PHP programming and Javascript, as well as other languages with Javascript.

  • 1

    Since you have Angularjs and Backbonejs, I advise you to create a REST service in PHP return a JSON, then call your API/Service using AJAX to then create your chart.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

7

I believe you are putting the car before the horse. You do not choose the technology you will use and then see what problem you will solve. That’s why you made the initial statement that it makes no sense at all:

We know that to have an efficient and dynamic system, we recommend using Javascript, such as, Jquery, Angularjs, Backbone.js and others

This is completely incorrect. Precisely because you think the technology is what’s important.

Javascript can even be used to access a local "database", but it’s probably not what you want. JS can also be used through Node.js on the server to access a database, but it’s also not what you want.

So if you’re going to access a database on the server do it with PHP and establish communication between the client and the server with web technologies. If JS can (not always right) help give a better experience in this communication, then use it in the client to facilitate.

Make sure you don’t put any JS inside PHP. You just put any text in PHP, which you happen to know is a JS code, but it will be sent to the client and will not run by PHP. It is not a question of being illegible, it is not feasible. In addition to being illegible and odd.

First ensure that the server side is getting the data you need. Then do a basic test where the client takes this data in the simplest way possible, only using HTML. Then get sophisticated using techniques where Javascript is used, with or without frameworks.

As the question is full of false premises, it is difficult to help more, but it is possible to post new questions with specific questions at each stage of the desired solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.