更新时间:2023-08-23 10:59:04
jQuery中的Ajax就像这样:
Ajax in jQuery works like this:
var myData=1;
$.ajax({
type:'POST',//type of ajax
url:'mypage.php',//where the request is going
data:myData,//the variable you want to send
beforeSend:function(xhr){//as a standard, I add this to validate stuff
if(someThingWrong===true)xhr.abort//aborts xhttpRequest
},
success:function(result){
//result is your result from the xhttpRequest.
}
});
这不会刷新您的页面,但会向指定的网址发送"POST".在您指定的页面上,您想做任何您想做的事情并说返回结果.在我的示例中,我将做一些简单的事情:
This will not refresh your page but send a 'POST' to the url specified. On your specified page you want to do whatever it is you want to do and say return a result. In my example I'll do something simple:
if($_POST['myData']===1)return True;
这是使用jQuery进行AJAX请求的基础.
That's the basics of an AJAX request using jQuery.
编辑!
启动AJAX脚本: 我只是在猜测,因为我不知道您的html中的元素还是您的脚本中的任何元素!因此,您必须进行调整!
initiating an AJAX script: I'm only guess as I don't know your elements within your html nor your scripts what so ever! So you'll have to make adjustments!
$('button.dislike').click(function(){
$.ajax({
type:'POST',
url:'disliked.php',
data:{dislike:$(this).attr('id')},
success:function(result){
$(this).prev('span').append(result);
}
});
});
PHP: 不要使用mysql,它已被贬值,被认为是不正确的做法,我也不知道为什么在查询中使用sprintf? :S
PHP: don't use mysql, it's now been depreciated and is considered bad practise, I also don't know why're using sprintf on the query? :S
$DBH=new mysqli('location','username','password','database');
$get=$DBH->prepare("SELECT dislike FROM random WHERE ids=?");
$get->bind_param('i',$_POST['dislike']);
$get->execute();
$get->bind_result($count);
$get->close();
$update=$DBH->prepare('UPDATE random SET dislike=? WHERE ids=?');
$update->bind_param('ii',++$count,$_POST['dislike']);//if you get an error here, reverse the operator to $count++.
$update->execute();
$update->close();
return String $count++;
仅当HTML中存在一系列ID与数据库中的ID匹配的按钮时,此方法才有效.所以
This will only work if there in your HTML there is a series of buttons with ID's matching those in your database. So
$get=$DBH->prepare('SELECT ids FROM random');
$get->execute();
$get->bind_result($ids);
while($get->fetch()){
echo"<button class='dislike' id='".$ids."'>Dislike this?</button>";
}
希望您对我如何管理不喜欢的按钮系统XD大声笑有个大概的认识
Hope you get the general idea of how I'm managing your dislike button system XD lol