views:

866

answers:

4

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?

+6  A: 

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.

ck
agreed. Presumably the prices are already included on the page already so this shouldn't be too difficult
Jonathan Fingland
You wouldn't even need to have them in hiddens, you could simply use well marked divs/spans. That way the displayed price will match the price selected every time.
acrosman
If you go this route, it's worth mentioning: If this is part of a 'shopping cart', remember to update and get totals to charge the user from the back-end, and not from the "grand total" that was generated for the user. An "evil hacker" could simply change the price displayed on the page, and your shopping system should be able to know the difference.
anonymous coward
@anonymous coward - if only sites did it that way...
ck
+1  A: 

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

rpflo
it took a time to realize that 'xhr' referred to XMLHTTPRequest. :-)
Javier
Sorry :) Gotten a little to used to webkit's console!
rpflo
This is a brilliant idea. The number of services will never be greater than 10.What I should do it create hidden fields whose names are appended with the ID of the services record in the DB so it becomes to easy to know as to which hidden field I take values from.Thanks man. A Gr8 idea!
Yogi Yang 007
A: 

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.

acrosman
A: 

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.

Dmitri Farkov