HTML :
HTML:
<a href="/profile.php?user={$username}&action_request="friend_request">
Friend request
</a>
PHP :
PHP:
if(isset($_GET['user']) && isset($_GET['action_request'])) {
if(strcmp($_GET['action_request'],"friend_request") == 0) {
insert_friendship($id_user1,$id_user2);
}
}
and the function for insert is :
插入的函数是:
//sql string
if(sql_insert()) {
return "Friendship request sent!";
} else {
return "Friendship request failed!"; }
P.S I am using smarty engine for templating.
P。我正在用智能引擎做模板。
How do i take the url from <a>
tag with ajax and send it to the php page to call the function then ajax to wait for the respond and send it to the html.
如何使用ajax从标记获取url并将其发送到php页面以调用函数,然后使用ajax等待响应并将其发送到html。
2 个解决方案
#1
2
include jquery in your page.
在页面中包含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>
#2
1
For using ajax you can use jQuery.ajax() http://api.jquery.com/jQuery.ajax/
要使用ajax,可以使用jQuery.ajax() http://api.jquery.com/jQuery.ajax/
And using ajax u will have to call the ajax function on click of the <a>
tag and will have to specify the url on the ajax function.
使用ajax u必须在单击标记时调用ajax函数,并必须在ajax函数上指定url。
OR
或
If you want to take the url from <a>
tag with ajax and send it to the php page you will have to prevent the default click of the <a>
tag using e.preventDefault();
and call ajax from using the url
如果您想使用ajax从标记获取url并将其发送到php页面,您必须防止使用e.preventDefault()默认单击标记;并通过url调用ajax
Try something like this and work around
试试这样的方法,然后尝试一下
$('selector').click(function(){
e.preventDefault();
var url = $(this).attr('href');
$.ajax({
url: url,
success: function(data) {
//do something
}
});
})
Call this function inside $(document).ready().
在$(document).ready()中调用此函数。
#1
2
include jquery in your page.
在页面中包含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>
#2
1
For using ajax you can use jQuery.ajax() http://api.jquery.com/jQuery.ajax/
要使用ajax,可以使用jQuery.ajax() http://api.jquery.com/jQuery.ajax/
And using ajax u will have to call the ajax function on click of the <a>
tag and will have to specify the url on the ajax function.
使用ajax u必须在单击标记时调用ajax函数,并必须在ajax函数上指定url。
OR
或
If you want to take the url from <a>
tag with ajax and send it to the php page you will have to prevent the default click of the <a>
tag using e.preventDefault();
and call ajax from using the url
如果您想使用ajax从标记获取url并将其发送到php页面,您必须防止使用e.preventDefault()默认单击标记;并通过url调用ajax
Try something like this and work around
试试这样的方法,然后尝试一下
$('selector').click(function(){
e.preventDefault();
var url = $(this).attr('href');
$.ajax({
url: url,
success: function(data) {
//do something
}
});
})
Call this function inside $(document).ready().
在$(document).ready()中调用此函数。