HTML:
<a href="/profile.php?user={$username}&action_request="friend_request">
Friend request
</a>
PHP:
if(isset($_GET['user']) && isset($_GET['action_request'])) {
if(strcmp($_GET['action_request'],"friend_request") == 0) {
insert_friendship($id_user1,$id_user2);
}
}
插入函数是:
//sql string
if(sql_insert()) {
return "Friendship request sent!";
} else {
return "Friendship request failed!"; }
P.S 我正在使用 smarty 引擎进行模板制作。
我如何从 <a>
获取 url用ajax标记并将其发送到php页面调用函数然后ajax等待响应并将其发送到html。
最佳答案
在您的页面中包含 jquery。
<script type="text/javascript">
document.getElementById("ajax_a").addEventListner("click", function(e){
e.preventDefault();
var uri = document.getElementById("ajax_a").getAttribute("href");
$.ajax({
url: uri,
type: "GET",
data: {},
dataType: "html",
success: function(data){
// data is the value returned by server.
},
error : function(){
alert("some error occured");
// data is the value returned by server.
}
});
});
</script>
<a id="ajax_a" href="/profile.php?user={$username}&action_request="friend_request">
Friend request
</a>
关于php - 如何调用ajax中的函数到php函数并等待响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10472083/