我想通过两个变量(下)到PHP/MySQL“更新$表SET ....”而不刷新页面。jQuery/JavaScript的AJAX调用传递变量onClick的div
我要上点击div
通过下列变量
$read=0;
$user=$userNumber;
的div
基本上显示邮件已读这样的话应该改变颜色。
请问这样做的最好方法是什么?
我想通过两个变量(下)到PHP/MySQL“更新$表SET ....”而不刷新页面。jQuery/JavaScript的AJAX调用传递变量onClick的div
我要上点击div
通过下列变量
$read=0;
$user=$userNumber;
的div
基本上显示邮件已读这样的话应该改变颜色。
请问这样做的最好方法是什么?
这里有一些代码使用jQuery发布到页面并处理json响应。您将不得不创建一个PHP页面,该页面将收到发布请求并返回您想要的任何内容。
$(document).ready(function() {
$.post("/yourpath/page.php", { read: "value1", user: $userNumber}, function (data) {
if (data.success) {
//do something with the returned json
} else {
//do something if return is not successful
} //if
}, "json"); //post
});
我不确定我的理解。 见$.load();
用更新代码创建一个新的php文件,然后只是返回一个json,如果它的工作与否。您可以使用$ .getJSON jQuery函数进行设置。
创建一个PHP/JSP/.net页面有两个参数
mywebsite.com/ajax.php?user=XXX&secondParam=ZZZZ
武官onClick事件基于它的ID给DIV
$.get("ajax.php?user=XXX&secondParam=ZZZZ". function(data){
// here you can process your response and change DIV color if the request succeed
});
要选择的DOM元素在jQuery中,只需执行以下操作:
$("#TheIdOfYourElement")
你的情况
$("#messageMenuUnread")
现在,听的被点击时它,
$("#messageMenuUnread").click(function(){
//DO SOMETHING
}
现在,对于AJAX的乐趣。您可以阅读http://api.jquery.com/category/ajax/更多技术细节的文件,但是这是它归结为
$("#TheIdOfYourImage").click(function(){
$.ajax({
type: "POST", // If you want to send information to the PHP file your calling, do you want it to be POST or GET. Just get rid of this if your not sending data to the file
url: "some.php", // The location of the PHP file your calling
data: "name=John&location=Boston", // The information your passing in the variable1=value1&variable2=value2 pattern
success: function(result){ alert(result) } // When you get the information, what to do with it. In this case, an alert
});
}
至于颜色变化,可以使用.css()
方法
$("#TheIdOfAnotherElement").css("background-color","red")
使用更改CSS jQuery.ajax()
你的代码看起来像
<!DOCTYPE html>
<head>
</head>
<body>
<!-- your button -->
<div id="messageMenuUnread"></div>
<!-- place to display result -->
<div id="frame1" style="display:block;"></div>
<!-- load jquery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
//attach a function to messageMenuUnread div
$('#messageMenuUnread').click (messageMenuUnread);
//the messageMenuUnread function
function messageMenuUnread() {
$.ajax({
type: "POST",
//change the URL to what you need
url: "some.php",
data: { read: "0", user: "$userNumber" }
}).done(function(msg) {
//output the response to frame1
$("#frame1").html("Done!<br/>" + msg);
});
}
}
</script>
</body>