适合这种情况的技术是什么? [关闭]

适合这种情况的技术是什么?  [关闭]

问题描述:

I am currently programming a community forum and i have a scenario where a user can say thank you to a specific post by clicking a button

that button should trigger multiple sequences checking things with mysql databases and such but i cannot think of an effective way to load/initiate a separate php file without leaving the page, or needing to refresh it.

i can use javascript to give the impression that your click went thru and was properly processed without needing to refresh the page,but i cannot use javascript to run mysql queries or something of that kind.

so what exacly can i do here?

this is very similar to pressing "upvote/downvote" on StackOverflow since its instant and doesnt require reloading of the page.. so what is SO using for example to achieve this?

additional information: i cannot make the server load the php file or something of that sort because i need the user session for it to work!

thank you alot for your help.

我正在编写一个社区论坛,我有一个用户可以说 谢谢你的场景 em>到一个特定的帖子,点击一个按钮 p>

该按钮应该触发多个序列检查mysql数据库等等,但我想不出有效的方法来加载/启动一个单独的 php文件没有离开页面,或需要刷新它。 p>

我可以使用javascript给人的印象是你的点击通过并且处理得当而无需刷新页面,但我 不能使用javascript来运行mysql查询或类似的东西。 p>

那么我能在这里做些什么呢? p>

这非常类似于按“ upvote / downvote“在StackOverflow上,因为它的瞬间并不需要重新加载页面..所以有什么用于实现这个目的? p>

附加信息 strong>: 我无法使服务器加载php文件或类似的东西因为 使用我需要用户会话才能工作! p>

非常感谢你的帮助。 p> div>

It sounds like you just want to use an Ajax call which allows Javascript in your page to send an HTTP request (e.g. an URL or form) to a server. The server (your PHP in this case) processes that request and returns the response back to the Javascript in the page, all without doing anything to the current page. The user session (assuming it is cookie-based) will be sent with the request automatically so your PHP server will have access to the session.

The Javascript in the page can then do anything it wants with the response (nothing, modify the contents of the page, report new status, etc...).

Here's an intro to Ajax: https://developer.mozilla.org/en-US/docs/AJAX

This is a widely used technology and it is exactly what is used by StackOverflow when you upvote or downvote.

I might also mention that basic Ajax requests are fairly straightforward in plain Javascript (you can usually copy an example to get started), but it is often useful to use a library such as jQuery which offers many features on top of the Ajax infrastructure which can be useful and save you a lot of time.