最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - how to retrieve data from MySQL using javascript - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited May 26, 2009 at 15:38 Alex Martelli 884k174 gold badges1.2k silver badges1.4k bronze badges asked May 26, 2009 at 15:12 Yogi Yang 007Yogi Yang 007 5,25110 gold badges60 silver badges80 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 6

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.

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 http://dojotoolkit. http://jquery. http://www.prototypejs

If your data is too plex 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.

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

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.

发布评论

评论列表(0)

  1. 暂无评论