如何在 ajax 中调用函数到 php 函数并等待响应
How to call a function in ajax to php function and wait for response
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!"; }
附言我正在使用智能引擎进行模板。
如何使用 ajax 从<a>
标签中获取 url 并将其发送到 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>
要使用 ajax,您可以使用 jQuery.ajax(( http://api.jquery.com/jQuery.ajax/
使用 ajax,您必须在单击 <a>
标记时调用 ajax 函数,并且必须在 ajax 函数上指定 url。
或
如果你想从 ajax 标签<a>
中获取 url 并将其发送到 php 页面,你必须使用 e.preventDefault();
阻止默认点击 <a>
标签,并调用 ajax 使用 url
尝试这样的事情并解决方法
$('selector').click(function(){
e.preventDefault();
var url = $(this).attr('href');
$.ajax({
url: url,
success: function(data) {
//do something
}
});
})
在 $(document(.ready(( 中调用此函数。
相关文章:
- 通过Ajax将JavaScript函数传递给PHP文件
- 在javascript函数中使用php变量
- 调用php数组中的JS函数
- 执行php函数的onclick事件的其他替代方案
- 使用ajax用jquery调用php函数
- 如何通过ajax增加/减少PHP变量值并重新加载函数
- php函数的ajax html$_POST值返回null
- 如何检查这个代码点火器php函数是否工作,该函数是否在ajax url中使用
- 无法识别从php创建的Js函数
- PHP/JS-EchoJs函数PHP curl发布后的通知
- 如果函数PHP中的条件为true,则禁用链接
- Jquery在Wordpress中不起作用;函数php
- Ajax 中的函数 php
- 函数.php当变量未硬编码时脚本中断
- 可以't调用函数php,ajax
- 获取我的数据库值作为字符串传递给我的javascript函数PHP
- 从下拉菜单中发送和设置3个选定的值到JavaScript函数PHP
- 通过触发AJAX函数——PHP
- 如何从ajax函数(PHP)为js变量赋值
- 通过参数将php变量传递给javascript触发器函数PHP,JAVASCRIPT