Php-Ajax喜欢/不喜欢按钮
我通过对脚本使用“喜欢/不喜欢”按钮从internet搜索中找到了此脚本 当用于单个帖子时,一切都很好,但是 我想对来自while cycle的所有文档使用此分级脚本 例如,我有一个新闻脚本,列出了所有的新闻,并且在标题附近可以看到喜欢/不喜欢按钮。(比如stackoverflow网页)我想对所有这些内容分别打分。在这个脚本中,我无法在同一页中分隔文章idPhp-Ajax喜欢/不喜欢按钮,php,ajax,social-media-like,Php,Ajax,Social Media Like,我通过对脚本使用“喜欢/不喜欢”按钮从internet搜索中找到了此脚本 当用于单个帖子时,一切都很好,但是 我想对来自while cycle的所有文档使用此分级脚本 例如,我有一个新闻脚本,列出了所有的新闻,并且在标题附近可以看到喜欢/不喜欢按钮。(比如stackoverflow网页)我想对所有这些内容分别打分。在这个脚本中,我无法在同一页中分隔文章id 您能帮我一下吗?首先,您需要为每个按钮指定文章的目的,您可以使用data-attribute轻松完成 <button class="
您能帮我一下吗?首先,您需要为每个按钮指定文章的目的,您可以使用data-attribute轻松完成
<button class="votebutton" data-article="1" data-vote="1">Up vote</button>
<button class="votebutton" data-article="1" data-vote="-1">Down vote</button>
<span class="votes" data-article="1"><?=//print the current number of votes of this article//?>
现在您需要rateArticle()函数,该函数如下所示:
function rateArticle(vote, article){
var data = 'vote='+rating+'&articleID='+article;
$.ajax({
type: 'POST',
url: 'rateArticle.php',
data: data,
success: function(votes){
$('.vote[data-article='+article+']').html(votes);
}
});
}
$vote = $_POST['vote'];
$articleID = $_POST['articleID'];
$currentVotes = ; //fill it with the votes that the article currently have
if($vote != "-1" || vote != "1"){
//goddamn script kids messing with us again, let's put them back where they belong
header("Location: http://www.lemonparty.com"); //probably not this, but you can think of something
}
if(userHasAlreadyVoted()){ //if user has already voted we just return the current number of votes
echo $currentVotes;
}else{
$newVotes = $currentVotes + $vote;
//save newVotes to db...
echo $newVotes;
}
在服务器端,您将发布到的rateArticle.php文件如下所示:
function rateArticle(vote, article){
var data = 'vote='+rating+'&articleID='+article;
$.ajax({
type: 'POST',
url: 'rateArticle.php',
data: data,
success: function(votes){
$('.vote[data-article='+article+']').html(votes);
}
});
}
$vote = $_POST['vote'];
$articleID = $_POST['articleID'];
$currentVotes = ; //fill it with the votes that the article currently have
if($vote != "-1" || vote != "1"){
//goddamn script kids messing with us again, let's put them back where they belong
header("Location: http://www.lemonparty.com"); //probably not this, but you can think of something
}
if(userHasAlreadyVoted()){ //if user has already voted we just return the current number of votes
echo $currentVotes;
}else{
$newVotes = $currentVotes + $vote;
//save newVotes to db...
echo $newVotes;
}
这就是问题所在。首先,您需要为每个按钮指定文章的目的,您可以使用data-attribute轻松完成
<button class="votebutton" data-article="1" data-vote="1">Up vote</button>
<button class="votebutton" data-article="1" data-vote="-1">Down vote</button>
<span class="votes" data-article="1"><?=//print the current number of votes of this article//?>
现在您需要rateArticle()函数,该函数如下所示:
function rateArticle(vote, article){
var data = 'vote='+rating+'&articleID='+article;
$.ajax({
type: 'POST',
url: 'rateArticle.php',
data: data,
success: function(votes){
$('.vote[data-article='+article+']').html(votes);
}
});
}
$vote = $_POST['vote'];
$articleID = $_POST['articleID'];
$currentVotes = ; //fill it with the votes that the article currently have
if($vote != "-1" || vote != "1"){
//goddamn script kids messing with us again, let's put them back where they belong
header("Location: http://www.lemonparty.com"); //probably not this, but you can think of something
}
if(userHasAlreadyVoted()){ //if user has already voted we just return the current number of votes
echo $currentVotes;
}else{
$newVotes = $currentVotes + $vote;
//save newVotes to db...
echo $newVotes;
}
在服务器端,您将发布到的rateArticle.php文件如下所示:
function rateArticle(vote, article){
var data = 'vote='+rating+'&articleID='+article;
$.ajax({
type: 'POST',
url: 'rateArticle.php',
data: data,
success: function(votes){
$('.vote[data-article='+article+']').html(votes);
}
});
}
$vote = $_POST['vote'];
$articleID = $_POST['articleID'];
$currentVotes = ; //fill it with the votes that the article currently have
if($vote != "-1" || vote != "1"){
//goddamn script kids messing with us again, let's put them back where they belong
header("Location: http://www.lemonparty.com"); //probably not this, but you can think of something
}
if(userHasAlreadyVoted()){ //if user has already voted we just return the current number of votes
echo $currentVotes;
}else{
$newVotes = $currentVotes + $vote;
//save newVotes to db...
echo $newVotes;
}
这就是问题所在……我已经编写了这段代码,它对于在p.h.p-code中使用ajax的人来说效果很好:) 我将它分为三个页面,分别命名为test1.php、test1.js、test1ppp.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("school");
$id = $_GET['status_id'];
$q = mysql_query("SELECT * from s_user WHERE u_id = $id");
$r = mysql_fetch_array($q);
$fetch_status = $r['u_status'];
if($fetch_status == 1){
mysql_query("UPDATE s_user SET u_status = 0 WHERE u_id = '$id'");
}else{
mysql_query("UPDATE s_user SET u_status = 1 WHERE u_id = '$id'");
}
?>
<table id="status_id">
<?php
$qry = mysql_query("SELECT * from s_user");
while($row = mysql_fetch_array($qry)){
$uid = $row['u_id'];
$status = $row['u_status'];
$uname = $row['u_uname'] . "<br />";
?>
<tr>
<td><?php echo $uname; ?></td>
<td><div id=""><?php echo $status; ?></div></td>
<td><input type="button" onclick="return change_status(this.value);" value="<?php echo $uid; ?>"></td>
<?php } ?>
</tr>
</table>
test1.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("school");
?>
<html>
<head>
<script type="text/javascript" src="test1.js"></script>
</head>
<body>
<form action="" method="post" enctype="">
<table id="status_id">
<?php
$qry = mysql_query("SELECT * from s_user");
while($row = mysql_fetch_array($qry)){
$uid = $row['u_id'];
$status = $row['u_status'];
$uname = $row['u_uname'] . "<br />";
?>
<tr>
<td><?php echo $uname; ?></td>
<td><div id=""><?php echo $status; ?></div></td>
<td><input type="button" onclick="return change_status(this.value);" value="<?php echo $uid; ?>"></td>
<?php } ?>
</tr>
</table>
</form>
</body>
</html>
test1ppp.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("school");
$id = $_GET['status_id'];
$q = mysql_query("SELECT * from s_user WHERE u_id = $id");
$r = mysql_fetch_array($q);
$fetch_status = $r['u_status'];
if($fetch_status == 1){
mysql_query("UPDATE s_user SET u_status = 0 WHERE u_id = '$id'");
}else{
mysql_query("UPDATE s_user SET u_status = 1 WHERE u_id = '$id'");
}
?>
<table id="status_id">
<?php
$qry = mysql_query("SELECT * from s_user");
while($row = mysql_fetch_array($qry)){
$uid = $row['u_id'];
$status = $row['u_status'];
$uname = $row['u_uname'] . "<br />";
?>
<tr>
<td><?php echo $uname; ?></td>
<td><div id=""><?php echo $status; ?></div></td>
<td><input type="button" onclick="return change_status(this.value);" value="<?php echo $uid; ?>"></td>
<?php } ?>
</tr>
</table>
我已经编写了这段代码,它在p.h.p-code中使用ajax时效果很好:)
我将它分为三个页面,分别命名为test1.php、test1.js、test1ppp.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("school");
$id = $_GET['status_id'];
$q = mysql_query("SELECT * from s_user WHERE u_id = $id");
$r = mysql_fetch_array($q);
$fetch_status = $r['u_status'];
if($fetch_status == 1){
mysql_query("UPDATE s_user SET u_status = 0 WHERE u_id = '$id'");
}else{
mysql_query("UPDATE s_user SET u_status = 1 WHERE u_id = '$id'");
}
?>
<table id="status_id">
<?php
$qry = mysql_query("SELECT * from s_user");
while($row = mysql_fetch_array($qry)){
$uid = $row['u_id'];
$status = $row['u_status'];
$uname = $row['u_uname'] . "<br />";
?>
<tr>
<td><?php echo $uname; ?></td>
<td><div id=""><?php echo $status; ?></div></td>
<td><input type="button" onclick="return change_status(this.value);" value="<?php echo $uid; ?>"></td>
<?php } ?>
</tr>
</table>
test1.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("school");
?>
<html>
<head>
<script type="text/javascript" src="test1.js"></script>
</head>
<body>
<form action="" method="post" enctype="">
<table id="status_id">
<?php
$qry = mysql_query("SELECT * from s_user");
while($row = mysql_fetch_array($qry)){
$uid = $row['u_id'];
$status = $row['u_status'];
$uname = $row['u_uname'] . "<br />";
?>
<tr>
<td><?php echo $uname; ?></td>
<td><div id=""><?php echo $status; ?></div></td>
<td><input type="button" onclick="return change_status(this.value);" value="<?php echo $uid; ?>"></td>
<?php } ?>
</tr>
</table>
</form>
</body>
</html>
test1ppp.php
<?php
mysql_connect("localhost","root","");
mysql_select_db("school");
$id = $_GET['status_id'];
$q = mysql_query("SELECT * from s_user WHERE u_id = $id");
$r = mysql_fetch_array($q);
$fetch_status = $r['u_status'];
if($fetch_status == 1){
mysql_query("UPDATE s_user SET u_status = 0 WHERE u_id = '$id'");
}else{
mysql_query("UPDATE s_user SET u_status = 1 WHERE u_id = '$id'");
}
?>
<table id="status_id">
<?php
$qry = mysql_query("SELECT * from s_user");
while($row = mysql_fetch_array($qry)){
$uid = $row['u_id'];
$status = $row['u_status'];
$uname = $row['u_uname'] . "<br />";
?>
<tr>
<td><?php echo $uname; ?></td>
<td><div id=""><?php echo $status; ?></div></td>
<td><input type="button" onclick="return change_status(this.value);" value="<?php echo $uid; ?>"></td>
<?php } ?>
</tr>
</table>
当然可以。你有什么,你尝试过什么,什么对你不起作用?我成功地给一篇文章打分,我没有单独给一页中列出的文章打分。剧本只是第一篇文章。我也要像stackoverflow一样思考。页面中列出了帖子和答案,每个人都可以通过点击喜欢或不喜欢按钮来给每个帖子打分。你有什么,你尝试过什么,什么对你不起作用?我成功地给一篇文章打分,我没有单独给一页中列出的文章打分。剧本只是第一篇文章。我也要像stackoverflow一样思考。页面中列出了帖子和答案,每个人都可以通过点击喜欢或不喜欢按钮对每个帖子进行评分