使用jquery / ajax执行PHP函数并显示消息
use jquery / ajax to execute php function and display message
我想更新一个数据库字段。我可以通过下面的代码实现它,但这需要刷新页面,并且我无法显示有关操作结果的消息。
我尝试使用一些JQ脚本没有成功(它对我来说是如此神秘,然而,它是在我的学习计划只是还没有)。
你能帮我用ajax/jquery实现这个吗?
index . php
include ("functions.php");
if (isset($_GET['user_id'])) {
$user_id = $_GET['user_id'];
update_time($user_id);
}
?>
<html>
<head>
<title>Update Time</title>
</head>
<body>
<div id="msg"></div>
<a id="update_link" href="index.php?user_id=user_id">Update Time</a>
</body>
</html>
显然也
<?php
function user_exist(){
//run an SQL query to check if the user exist
if (exist)
return true;
else
return false;
}
function update_time($user_id){
if(user_exist())
//display a message in #msg that the time was updated
//update the database
else
//display a message in #msg that the user does not exist
}
?>
脚本$("a#update_link").click( function() {
$.post( $(this).attr("href"),
function(data) {
if (data == "Time Updated") {
window.location = "index.php";
}
$("div#msg").html(data);
});
$(this).click( function() {
return false;
});
});
HTML (index . php)
为了使用jQuery,你必须首先在你的代码中包含脚本文件。
<html>
<head>
<title>Update Time</title>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="function.js"></script>
</head>
<body>
<div id="msg"></div>
<a id="update_link" href="index.php?user_id=user_id">Update Time</a>
</body>
</html>
JS (function.js)
接下来,您想要将'click'事件处理程序附加到#update_link
元素。当您将事件处理程序附加到链接时,您希望防止它使用e.preventDefault()
重定向到另一个页面的默认行为。
// shorter syntax for document.ready
$(function () {
// attach a click event handler
$('#update_link').click(function(e) {
// prevent the click from redirecting
e.preventDefault();
// make an AJAX request
$.post($(this).attr('href'), function (res) {
// handle the response (*) by storing the message into the DIV#message
$('#msg').html(res);
});
});
});
PHP 现在,您正在向相同的URL (index.php
)发出AJAX请求。通常最好有一个单独的脚本来处理AJAX请求——但我们将使用您当前的设置。这意味着您的页面需要能够处理两种不同类型的请求:AJAX请求和非AJAX请求。
当它是一个AJAX请求时,我们只想输出一个HTML消息。如果不是,我们希望输出HTML页面。
(index . php)
那么,首先让我们再次修改你的页面。
<?php
include 'functions.php';
// check if the request was made via AJAX
if (is_ajax() && isset($_GET['user_id'])) {
// update and print (not return) a message
update_time($_GET['user_id']);
// stop the script from outputting the rest
exit;
}
?>
(显然也)
接下来,让我们创建一个新函数(我们刚刚在上面使用过),以便检测我们是否正在接收AJAX请求。
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
}
最后,确保您print
(或echo
)您的消息。这将是AJAX响应(*),它将被发送回您的JavaScript。
function update_time($user_id){
if (user_exist()) {
//update the database
//print a message that the time was updated
} else {
//print a message that the user does not exist
}
}
为了防止页面重新加载,您可以将href属性更改为:
<a id="update_link" href="#" data-link="index.php?user_id=user_id">Update Time</a>
和更新你的JavaScript post URL:
$.post( $(this).data("link"),
或者,像这样停止单击函数中的默认事件:
$("a#update_link").click( function(event) {
event.preventDefault();
- 更新URL时使用Javascript函数显示日期
- 使用 js 函数显示 HTML 表单的结果
- Javascript日期函数显示错误的日期
- I'm试图使用onclick函数显示函数的结果.(javascript)
- 如何使用 JavaScript 函数显示表单 asp.net
- 通过脚本在中隐藏元素,然后使用函数显示
- 向对象原型添加函数会导致函数显示在所有“OBJ 中的 X”循环中
- 图像 onclick() 函数显示带有表单详细信息的 dailog 窗口
- Javascript 日期未显示为相等,尽管 getTime() 函数显示日期相等
- Javascript函数显示用户输入数组
- PHP中用于显示模态的Echo Javascript函数显示不正确
- 使用构造函数显示模块模式
- JQuery函数显示依赖于隐藏HTML输入元素值的元素
- 为什么这个javascript使用getTime函数显示负数
- fancybox iframe内容未通过jquery函数显示
- HTML函数显示为纯文本
- 使用JavaScript函数显示日历
- Titanium,Js函数显示XML文本字段文本
- 使用JavaScript函数显示flash
- replaceWith函数显示了奇怪的结果