How to get data from MySQL using javascript
I have a webpage encoded in PHP. This page is for submitting a request for services. All services are stored in two separate tables along with prices.
When a user selects various services, I want to get the prices for the selected service and show it based on the service, as well as the total of all services.
How can we do this?
a source to share
I'm not really sure what you are going to do, but you cannot fetch anything from MySQL using Javascript, but you can request a php script via xhr that will fetch the data and then process (add it, etc.) via javascript ...
If the number of services / prices is small, you can load it into the page first and then use javascript to add things and leave the server alone.
There are several javascript frameworks and libraries out there that make this quite easy (xhr), have you looked into them?
http://mootools.net http://dojotoolkit.com http://jquery.com http://www.prototypejs.org
a source to share
If your data is too complex to preload as ck suggested, you can create a PHP page that generates JSON as your output . Using a library like jQuery , you can pull this data into a page for processing.
a source to share
Ajax is a php page that will perform MySQL operations. Make sure you really defend this stuff, avoid strings, and don't forget to check if the command is being sent you really want. Some users are smarter than others and can use "Drop *" using your ajax method.
Not a very secure idea IMO, but if something uses Ajax.
a source to share
Don't preload prices on the page; use AJAX to fetch them. You may be making some assumptions that are not necessarily true when the page loads, and in some cases, you can manipulate prices based on user-supplied data. Either way, you will most likely be using AJAX later.
Querying the database via AJAX is not difficult, it makes your code more flexible and very reliable.
Go for AJAX.
a source to share