这是我第一次使用jQuery AJAX所以请耐心等待。我正在尝试为我的网站创建一个类似于Facebook按钮的类似按钮。用户必须通过Facebook登录,这将从我的数据库中提取用户ID。当用户单击like按钮时,它会使用用户信息和页面信息将一行插入数据库。如果用户和页面已有条目,则会更新该行。
我的代码在我的localhost上运行得很好。我可以整天点击相似的按钮,图像会不断地从不同的方向来回变换。数据库每次都会更新,没有任何问题。
当我将其上传到我的实时服务器时,脚本运行完全正常。一旦我的页面完成加载,数据库就不再从点击中更新。图像将照常更改,我将收到警报(“成功”),但不会对实际数据库进行任何更改。
<?php
$tempstat = '1';
if ($stmt = $mysqli -> prepare("SELECT count(id)
FROM votes
WHERE businessid=? and status=? ")) {
$stmt -> bind_param('ss', $businessid, $tempstat);
$stmt -> execute();
$stmt->bind_result($likecount);
$stmt->fetch();
$stmt->close();
}
if ($likecount == '') { $likecount = 0; }
?>
<style>
#karmabar { padding-left: 4px; }
#counter { margin: 0 auto; text-align: center; width: 50px; height: 25px; background-image: url('/images/karma-counter.png'); }
p#counter { font-size: 12px; padding-top: 5px; }
.karma { margin: 0 auto; font-size: 16px; color: blue; text-align: center; padding: 5px; }
</style>
<?php
if(login_check($mysqli) == true) {
$userid = $_SESSION['userurl'];
if ($stmt = $mysqli -> prepare("SELECT status
FROM votes
WHERE userid=? and businessid=? ")) {
$stmt -> bind_param('ss', $userid, $businessid);
$stmt -> execute();
$stmt -> bind_result($like_status);
$stmt->fetch();
$stmt->close();
}
if ($like_status == '' or $like_status == NULL) { $like_status = '0'; }
?>
<script type="text/javascript">
$(document).ready(function() {
$('#like_post').click(function() {
$.ajax({
url: "/includes/like.php",
type: "GET",
data: 'userid=<?php echo $userid; ?>&businessid=<?php echo $businessid; ?>&like_status=1',
success: function() {
alert("Success");
},
error: function() {
alert("Something went wrong");
}
});
$('#counter').html(function(i, val) { return val*1+1 });
$('#like_post').hide();
$('#unlike_post').show();
});
$('#unlike_post').click(function() {
$.ajax({
url: "/includes/like.php",
type: "GET",
data: 'userid=<?php echo $userid; ?>&businessid=<?php echo $businessid; ?>&like_status=0',
success: function() {
alert("Success");
},
error: function() {
alert("Something went wrong");
}
});
$('#counter').html(function(i, val) { return val*1-1 });
$('#unlike_post').hide();
$('#like_post').show();
});
});
</script>
<div id="karmabar">
<table cellpadding="0px">
<?php if ($like_status == '0') { ?>
<tr><td><a href="javascript:;" id="unlike_post" class="hide"><img src="/images/karma-active.png" title="Undo Karma" /></a><a href="javascript:;" id="like_post"><img src="/images/karma-inactive.png" title="Spread Karma" /></a><td><p id="counter"><?php echo $likecount; ?></p></td></tr>
<?php } else { ?>
<tr><td><a href="javascript:;" id="unlike_post"><img src="/images/karma-active.png" title="Undo Karma" /></a><a href="javascript:;" id="like_post" class="hide"><img src="/images/karma-inactive.png" title="Spread Karma" /></a></td><td><p id="counter"><?php echo $likecount; ?></p></td></tr>
<?php } ?>
</table>
</div>
<?php } else { ?>
<div id="karmabar">
<table cellpadding="0px">
<tr><td><a href="#" onclick="alert('Login with Facebook to Spread Karma');" ><img src="/images/karma-inactive.png" title="Spread Karma" /></a><td><p id="counter"><?php echo $likecount; ?></p></td></tr>
</table>
</div>
$ businessid和$ userid都在页面上定义得更高。
like.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/includes/db_connect.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/includes/functions.php';
$userid = $_GET['userid'];
$businessid = $_GET['businessid'];
$IP = $mysqli->real_escape_string(getClientIP());
$like_status = $_GET['like_status'];
if ($stmt = $mysqli -> prepare("SELECT count(id)
FROM votes
WHERE userid=? and businessid=? ")) {
$stmt -> bind_param('ss', $userid, $businessid);
$stmt -> execute();
$stmt ->bind_result($count);
$stmt ->fetch();
$stmt ->close();
}
if ($count == '1') {
if ($stmt = $mysqli -> prepare("UPDATE votes
SET ip=?, status=?
WHERE userid=? and businessid=? ")) {
$stmt -> bind_param('ssss', $IP, $like_status, $userid, $businessid);
$stmt -> execute();
$stmt -> close();
$mysqli -> close();
}
}
if ($count == '0') {
if ($stmt = $mysqli -> prepare("INSERT INTO votes (userid, businessid, ip, status)
VALUES ( ?, ?, ?, ? ) ")) {
$stmt -> bind_param('ssss', $userid, $businessid, $IP, $like_status );
$stmt -> execute();
$stmt -> close();
$mysqli -> close();
}
}
?>
我感谢你们给我的任何帮助。就像我说的,我是这种编码的新手,正在寻找你可能有的任何指针。
答案 0 :(得分:0)
这听起来像连接错误。您是否已将连接参数从localhost更改为实际站点。即您要连接的数据库名称和密码?
答案 1 :(得分:0)
用以下代码替换整个AJAX函数:
$(document).ready(function() {
$('#like_post').click(function() {
$.post("/includes/like.php", {userid:<?php echo $userid; ?>, businessid:<?php echo $businessid; ?>, like_status:"1"}, function(data){
alert(data); //Check to see what is being returned
if(data == 1){ //Later, you can echo "1" in your PHP file on success
//This is success
alert("Success");
$('#counter').html(function(i, val) { return val*1+1 });
$('#like_post').hide();
$('#unlike_post').show();
} else {
//This is failure
alert("Failure");
}
});
});
$('#unlike_post').click(function() {
$.post("/includes/like.php", {userid:<?php echo $userid; ?>, businessid:<?php echo $businessid; ?>, like_status:"0"}, function(data){
alert(data); //Check to see what is being returned
if(data == 1){ //Later, you can echo "1" in your PHP file on success
//This is success
alert("Success");
$('#counter').html(function(i, val) { return val*1-1 });
$('#unlike_post').hide();
$('#like_post').show();
} else {
//This is failure
alert("Failure");
}
});
});
});
这将是PHP - 注意,我将$_GET
更改为$_POST
并在最后添加了回显。我解释了为什么之后。
<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/includes/db_connect.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/includes/functions.php';
$userid = $_POST['userid'];
$businessid = $_POST['businessid'];
$like_status = $_POST['like_status'];
$IP = $mysqli->real_escape_string(getClientIP());
if ($stmt = $mysqli -> prepare("SELECT count(id) FROM votes WHERE userid=? and businessid=? ")) {
$stmt -> bind_param('ss', $userid, $businessid);
$stmt -> execute();
$stmt ->bind_result($count);
$stmt ->fetch();
$stmt ->close();
echo "Got the count: " . $count;
}
if ($count == '1') {
echo "The Count is 1";
if ($stmt = $mysqli -> prepare("UPDATE votes SET ip=?, status=? WHERE userid=? and businessid=? ")) {
$stmt -> bind_param('ssss', $IP, $like_status, $userid, $businessid);
$stmt -> execute();
$stmt -> close();
$mysqli -> close();
echo "And the post is successful (1)";
}
}
if ($count == '0') {
echo "The Count is 0";
if ($stmt = $mysqli -> prepare("INSERT INTO votes (userid, businessid, ip, status) VALUES ( ?, ?, ?, ? ) ")) {
$stmt -> bind_param('ssss', $userid, $businessid, $IP, $like_status );
$stmt -> execute();
$stmt -> close();
$mysqli -> close();
echo "And the post is successful (0)";
}
}
echo "finished";
?>
基本上 - 我已经添加了一堆回声,你将能够倾听&#39;在返回数据时使用$.post
ajax调用。如果一切都成功,你应该得到一个句子,回复说&#34;得到计数:1TheCount是1并且帖子成功(1)完成&#34;。当帖子成功时,所有这些回声都应该开启。如果你没有得到其中一个句子,那就出问题了。
尝试此操作来解决问题,让我们知道会发生什么。