我正在实现类似“喜欢”按钮计数器的Facebook,所以每当用户点击按钮时,除了它之外的DIV标签从数据库中检索它并更新它(参见下面代码的逻辑)。但它不起作用。请参阅以下所有代码: -
的javascript:
function likeAJAX(strName)
{
If(strName == "")
{
document.getElementById('likeCount').innerHTML="";
return;
}
else
if(window.XMLHttpRequest)
{
xmlHTTP = new XMLHttpRequest();
}
else
{
xmlHTTP = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlHTTP.onreadystatechange = function()
{
if(xmlHTTP.readystate == 4 && xmlHTTP.status == 200)
{
document.getElementById('likeCount').innerHTML = xmlHTTP.responseText;
}
}
xmlHTTP.open("GET","getLikes.php?n=" + strName,true);
xmlHTTP.send();
}
HTML / PHP前端:
<img src="./images/like.jpg" style="cursor:hand;" onclick="likeAJAX(<?php echo $_GET['val']; ?>)"><?php echo $likeVal; ?> people like this</span>
PHP / MySQL的:
<?php
$q = $_GET['n'];
$temp;
$con = mysql_connect('localhost','xxxx','xxxx');
mysql_select_db("xxxx");
$SQLQuery = "SELECT * FROM likes where name= '" . $q . "'";
$res = mysql_query($SQLQuery);
while($data = mysql_fetch_array($res))
{
$temp = $data['likes'];
}
mysql_free_result($res);
mysql_close($con);
if($temp == "")
{
$temp = "0";
}
$intCount = int($temp);
$intCount++;
$con = mysql_connect('localhost','xxxx','xxxx');
mysql_select_db("xxxx");
$SQLQuery = "UPDATE likes set likes = '" . $intCount . "' WHERE busname = '". $q . "';";
$res = mysql_query($SQLQuery);
mysql_free_result($res);
mysql_close($con);
echo $intCount;
?>
对任何格式问题表示歉意。 我哪里错了?我看到它的方式,似乎在点击Like图像时,javascript函数似乎甚至没有被调用?
此致
Ochen
答案 0 :(得分:0)
尝试对你的js进行此修正:
function likeAJAX(strName) {
if (strName == "") {
document.getElementById('likeCount').innerHTML="";
return;
} else {
if(window.XMLHttpRequest) {
xmlHTTP = new XMLHttpRequest();
} else {
xmlHTTP = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlHTTP.onreadystatechange = function() {
if (xmlHTTP.readystate == 4 && xmlHTTP.status == 200) {
document.getElementById('likeCount').innerHTML = xmlHTTP.responseText;
}
}
xmlHTTP.open("GET","getLikes.php?n=" + strName,true);
xmlHTTP.send();
}
}