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.
我有一个用PHP编码的网页。此页面用于发布服务请求。所有服务都与价格一起存储在两个单独的表中。
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?
我们应该怎么做?
5 个解决方案
#1
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.
我不完全确定你的目的是什么,但你不能用Javascript从MySQL中获取任何东西,但你可以通过xhr请求一个PHP脚本来获取数据,然后处理它(加起来)等)通过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.
如果服务/价格的数量不是很大,您可以先将其全部加载到页面中,然后使用javascript添加内容并让服务器单独使用。
There are several javascript frameworks and libraries that make this sort of thing quite easy (xhr), have you looked into those?
有几个javascript框架和库使这类事情变得非常简单(xhr),你有没有看过这些?
http://mootools.net http://dojotoolkit.com http://jquery.com http://www.prototypejs.org
http://mootools.net http://dojotoolkit.com http://jquery.com http://www.prototypejs.org
#2
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.
您可以将所有变量添加到页面中作为隐藏的vairables,而不是对他们勾选的每个服务进行数据库查询,然后使用Javascript添加它们。这将减少开销。
#3
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.
如果您的数据太复杂而无法像ck建议的那样预加载,您可以创建一个生成JSON作为输出的PHP页面。使用像jQuery这样的库,然后您可以将该数据拉入页面进行处理。
#4
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.
Ajax是一个将执行MySQL操作的php页面。确保实际保护这些东西,逃避字符串,并确保检查发送的命令是否是您真正想要的命令。有些用户比其他用户更聪明,可能会使用你的ajax方法“Drop *”。
Not a very safe idea IMO, but if something use Ajax.
IMO不是一个非常安全的想法,但如果有什么东西使用Ajax。
#5
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.
不要在页面上预先加载价格;使用AJAX来检索它们。您可能会在页面加载时做出一些不一定正确的假设,在某些情况下,您可能希望根据用户提供的数据来操纵价格。无论如何,您以后更有可能使用AJAX。通过AJAX查询数据库并不难实现,它使您的代码更加灵活,而且速度非常快。去AJAX。
#1
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.
我不完全确定你的目的是什么,但你不能用Javascript从MySQL中获取任何东西,但你可以通过xhr请求一个PHP脚本来获取数据,然后处理它(加起来)等)通过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.
如果服务/价格的数量不是很大,您可以先将其全部加载到页面中,然后使用javascript添加内容并让服务器单独使用。
There are several javascript frameworks and libraries that make this sort of thing quite easy (xhr), have you looked into those?
有几个javascript框架和库使这类事情变得非常简单(xhr),你有没有看过这些?
http://mootools.net http://dojotoolkit.com http://jquery.com http://www.prototypejs.org
http://mootools.net http://dojotoolkit.com http://jquery.com http://www.prototypejs.org
#2
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.
您可以将所有变量添加到页面中作为隐藏的vairables,而不是对他们勾选的每个服务进行数据库查询,然后使用Javascript添加它们。这将减少开销。
#3
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.
如果您的数据太复杂而无法像ck建议的那样预加载,您可以创建一个生成JSON作为输出的PHP页面。使用像jQuery这样的库,然后您可以将该数据拉入页面进行处理。
#4
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.
Ajax是一个将执行MySQL操作的php页面。确保实际保护这些东西,逃避字符串,并确保检查发送的命令是否是您真正想要的命令。有些用户比其他用户更聪明,可能会使用你的ajax方法“Drop *”。
Not a very safe idea IMO, but if something use Ajax.
IMO不是一个非常安全的想法,但如果有什么东西使用Ajax。
#5
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.
不要在页面上预先加载价格;使用AJAX来检索它们。您可能会在页面加载时做出一些不一定正确的假设,在某些情况下,您可能希望根据用户提供的数据来操纵价格。无论如何,您以后更有可能使用AJAX。通过AJAX查询数据库并不难实现,它使您的代码更加灵活,而且速度非常快。去AJAX。