如何在不刷新页面的情况下从php获取信息
你好 抱歉,标题不好,但我不是 100% 确定我需要什么来解决这个问题,
我创建了一个欢迎页面,然后当您单击链接时,您会获得更多信息,例如:
<a href="?id=1">Click Me</a>
然后 php 会根据ID。 因此,在页面刷新后,收到的信息会重新加载到页面上,
我希望能够做的是,当用户单击链接时,使用 jquery 不允许链接运行,但仍然在后台运行 url(无需刷新)页面)
我不知道从哪里开始所以我真的希望你能帮忙
谢谢
hi
sorry for the bad title but I'm not 100% sure what I need for this problem
I created a welcome page and then when you click on links you get more information, for example:
<a href="?id=1">Click Me</a>
And then the php would get the information based on the id.
so the information received is reloaded on the page after the pages refreshes
what I would like to be able to do is when user clicks on the link, use jquery to not allow the link to run but still run the url in the background (without refreshing the page)
I have no idea where to start from so I really hope you could help
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简而言之,它称为 Ajax:通过 javaScript 向服务器发送 HTTP 请求,并接收包含结果、数据或其他信息的响应。
你提到了 jQuery,这里是关于它的文档:
http://api.jquery.com/jQuery.get/
http://api.jquery.com/jQuery.post/
是便捷方法,它使用预设选项封装 $.ajax。
http://api.jquery.com/category/ajax/ 是整个的概述jQuery 系统。
基本原理如下:
这将绑定一个元素以在单击时进行 Ajax 调用,然后使用该函数('success' 函数,在 $.ajax 中)来处理 json 数据。
让您的服务器使用 php 中的 json_encode 以 JSON 格式发回数据。请务必发回正确的标头,例如
网络上有很多资源可以用于学习 Ajax,这是一个很大的主题。祝你好运。
In a nutshell, it's called Ajax: sending an HTTP request to your server through javaScript, and receiving a response which can contain results, data, or other information.
You mention jQuery, here are the docs about that:
http://api.jquery.com/jQuery.get/
http://api.jquery.com/jQuery.post/
are convenience methods, which encapsulate $.ajax with preset options.
http://api.jquery.com/category/ajax/ is an overview of the whole system in jQuery.
The basics go like
This will bind an element to make an Ajax call on click, and then you use the function ('success' function, in $.ajax) to handle the json data.
Have your server send back the data in JSON by using json_encode in php. Be sure to send the right header back, like
There's a lot of resources on the web and SO for learning about Ajax, it's a big topic. Best of luck.
创建一个 JavaScript 函数,例如 sendData(linkId),然后每个标签都会有一个名为 sendData(this) 的 onclick 事件。然后,SendData(linkId) 可以对 php 文件执行 HTTPRequest(也称为异步或 AJAX 请求),我们将其称为 handler.php,它接收 GET 或 POST 方法。我更喜欢使用原型框架来做这种事情(你可以在prototypejs.org 上获取它)。
好吧,既然我已经说了这一切,让我们看看如何做到这一点的实质内容(出于说明目的而简化了方式)。
下载原型脚本,将其保存在您的服务器上(例如,prototype/prototype.js),然后将其放在 html 中的某个位置
您的标签如下所示:
Click me!
您需要 JavaScript 来执行此操作:
function sendData(tagId){
var url = 'handler.php?' + 'id=' + tagId;
var request = new AJAX.Request(url, {method = 'get'});
最后
,您需要一个 php 文件(我们称之为 handler.php),其中包含以下内容:
简而言之,但值得一提的是,您应该向用户提供某种反馈,表明单击链接会执行某些操作。否则,他会疯狂地点击链接,等待事情发生,而实际上它正在秘密地做它应该做的事情。为此,您可以让 php 脚本在末尾回显一些内容,例如“成功!”,然后将 onSuccess 参数添加到 JavaScript 的新 Ajax.Request 中。我将让您自行了解如何执行此操作,因为原型网站解释了如何接收来自处理程序的响应并将反馈放在 HTML 中的某个位置,而不需要用户刷新。
Make a JavaScript function, like sendData(linkId) and then each tag would have an onclick event called sendData(this). SendData(linkId) can then do an HTTPRequest (also known as an asynchronous or AJAX request) to a php file, let's call it handler.php, which receives GET or POST methods. I prefer using the prototype framework to do this kind of thing (you can get it at prototypejs.org).
Okay, now that I have said all that, let's look into the nitty-gritty of how to do this (way simplified for illustrative purposes).
Download the prototype script, save it on your server (like prototype/prototype.js, for example) and then put somewhere in your html
<script type='text/javascript' language='Javascript' src='prototype/prototype.js'></script>
Your tags would look like this:
<a id='exampleLink' onclick = 'sendData(this)'>Click me!</a>
You need JavaScript to do this:
function sendData(tagId){
var url = 'handler.php?' + 'id=' + tagId;
var request = new AJAX.Request(url, {method = 'get'});
}
Finally, you need a php file (let's call it handler.php) that has the following:
<?php
$tag_to_get = $_GET['tagId'];
do_a_php_function($tag_to_get);
?>
That's it in a nutshell, but it's worth mentioning that you should give your user some sort of feedback that clicking link did something. Otherwise he will click the link furiously waiting for something to happen, when it is actually doing just what its supposed to but in secret. You do that by making your php script echo something at the end, like 'Success!', and then add an onSuccess parameter to your JavaScript's new Ajax.Request. I'll let you read how to do that on your own because the prototype website explains how to receive a response from the handler and put the feedback somewhere in your HTML without making the user refresh.
您可以使用名为 $.get 的 jquery 函数来实现该行为...您可以在此处获取有关如何使用的更多信息 http://api.jquery.com/jQuery.get/
you can achieve that behavior with a jquery function called $.get ... you can get more information on how to use here http://api.jquery.com/jQuery.get/
如果您确实愿意(我认为您并不真的愿意),您可以使用 XMLHTTPRequest(封装在 jQuery.get 中)来方便地将内容加载到页面中,而无需刷新页面。您希望该标签上有
id
或class
,即Click Me
,然后:If you really want to (and I don't think you really do), you can use XMLHTTPRequest (wrapped in
jQuery.get
) to facilitate loading content into the page without page refreshing. You want anid
orclass
on that tag, i.e.<a href="?id=1" class="fetch">Click Me</a>
, and then:我建议您首先使用 AJAX。一个好地方是 http://www.w3schools.com/Ajax/Default.Asp
该链接还附带了一个方便的 AJAX ASP/PHP 示例 =))
祝你好运。
I would recommend you use AJAX to start with. A good place to being is http://www.w3schools.com/Ajax/Default.Asp
The link comes with a handy AJAX ASP/PHP Example too =))
Good Luck.