[英]Using jQuery/AJAX to run a php script on user click event
I've got a php script that I need to run using jQuery/AJAX when a user clicks a button. 我有一个PHP脚本,当用户单击按钮时,需要使用jQuery / AJAX运行该脚本。 I have a jQuery script that is supposed to fire when the user clicks the button.
我有一个jQuery脚本,应该在用户单击按钮时触发。 However, the event isn't actually firing.
但是,该事件实际上并未触发。 Can anyone assist me?
谁能帮助我?
instructor.php instructor.php
<div id="instructor">
<?php
echo "<img id=instructor_image src=" .$_SESSION["image"] .">";
echo "<h1>" .$_SESSION["user"] ."</h1>";
echo "<span><p>" .$_SESSION["program"] ."</p> - <h2>" .$_SESSION["role"] ."</h2></span>";
echo "<a href=mailto:" .$_SESSION["email"] .">" .$_SESSION["email"] ."</a>";
?>
<button class="button" name="logout" id="logout">Logout</button>
</div>
script.js 的script.js
$(document).ready(function(){
$("#logout").on("click", function(){
ajax = new XMLHttpRequest();
ajax.open("../php/logout.php", "POST", true);
ajax.send();
})
})
logout.php logout.php
<?php
require("../includes/header.php");
$loggedout = "UPDATE `$user_table` SET `logged_in`=0 WHERE `user_id`='$user_id'";
mysqli_query($connect, $loggedout);
session_unset();
session_destroy();
mysqli_close($connect);
header("Location: ../pages/logged_out.php");
?>
The session is started in the header and yes, the header is called on every page. 会话从标题开始,是的,标题在每个页面上都被调用。 I am also getting the following error on the
ajax.open()
line. 我也在
ajax.open()
行上收到以下错误。 NS_ERROR_ILLEGAL_VALUE:
there is something that is weird - header("Location: ../pages/logged_out.php");
header("Location: ../pages/logged_out.php");
do you want to do redirect after user click? 用户点击后是否要重定向? if so you should do it in your js.
如果是这样,您应该在js中进行。
i would change 我会改变
$(document).ready(function(){
$("#logout").on("click", function(){
$.post("../php/logout.php",{},function(response){
if(response.success == "1"){
location.replace("../pages/logged_out.php");
} else {
//handle error
}
},'json');
})
})
and php: 和PHP:
<?php
ob_start();
require("../includes/header.php");
ob_clean();
$loggedout = "UPDATE `$user_table` SET `logged_in`=0 WHERE `user_id`='$user_id'";
mysqli_query($connect, $loggedout);
session_unset();
session_destroy();
mysqli_close($connect);
echo json_encode(array('success'=>1));
?>
Because 因为
ajax.open("../php/logout.php", "POST", true);
is wrong. 是错的。 It is
.open(method, url, async)
. 它是
.open(method, url, async)
。
void open( DOMString method, DOMString url, optional boolean async, optional DOMString user, optional DOMString password );
So change the code to 因此将代码更改为
$("#logout").on("click", function(){
ajax = new XMLHttpRequest();
ajax.open("POST", "../php/logout.php", true);
ajax.onreadystatechange = function () {
if (xmlhttp.readyState==4) console.log("done");
}
ajax.send();
});
and since you are using jQuery, you can just do 由于您使用的是jQuery,因此您可以
$("#logout").on("click", function(){
$.post("../php/logout.php", function() { console.log("done") } );
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.