Reputation: 5251
I have a web page coded in PHP. This page is for posting a request for services. All services are stored in two separate tables along with prices.
As the user selects various services I want to fetch prices for the selected service and show it on per service basis and also a grand total of all services.
How can we do this?
Upvotes: 0
Views: 4729
Reputation: 4221
Don't pre-load the prices on the page; use AJAX to retrieve them. You might be making some assumptions that are not necessarily true when the page loads, and in some cases, you might want to manipulate the prices, based on the data supplied by the user. In any case, you are more likely to use AJAX later on.
Querying the database via AJAX is not difficult to implement, it makes your code more flexible, and it's very reliably fast.
Go for AJAX.
Upvotes: 0
Reputation: 9691
Ajax a php page that will do the MySQL operations. Make sure to actually secure that stuff, escape the strings, and make sure to check if the command being sent is the one you actually want. Some users are smarter than the rest and may go 'Drop *' using your ajax method.
Not a very safe idea IMO, but if something use Ajax.
Upvotes: 0
Reputation: 12900
If your data is too complex to pre-load as ck suggested, you can create a PHP page that generates JSON as your output. Using a library like jQuery you could then pull that data into the page for handling.
Upvotes: 0
Reputation: 13483
I'm not totally sure what you're going for, but you can't actually fetch anything from MySQL with Javascript, but you can request a php script via xhr that will fetch the data, and then deal with it (add it up, etc.) via javascript.
If the number of services/prices isn't huge, you could load it all in to the page in the first place then use javascript to add things up and leave the server alone.
There are several javascript frameworks and libraries that make this sort of thing quite easy (xhr), have you looked into those?
http://mootools.net http://dojotoolkit.com http://jquery.com http://www.prototypejs.org
Upvotes: 1
Reputation: 46475
Rather than a database query for every service they tick, you could add all the variables into the page as hidden vairables, then use Javascript to add them up. This would be much less overhead.
Upvotes: 6