0

我正在使用以下脚本在我的网站中构建一个 php-mysql 投票系统(如 youtube 中的投票系统)。我也在使用 ajax 来执行 newtest.php 中的 php。我自己测试了php,它工作得很好。现在我在 ajax 中使用鼠标单击来投票赞成或反对,但它不起作用。你能告诉我哪里出错了吗?(我是ajax的初学者)。我还想返回并显示自动更新的当前投票数。

$contentVar = $_POST['contentVar'];
if ($contentVar == "con1") {
    $db_host = "host";
    $db_username = "username";
    $db_pass = "pass";
    $db_name = "phphitscounter";

    @mysql_connect ("$db_host","$db_username","$db_pass") or die ("Could Not Connect To The Database");
    @mysql_select_db ("$db_name") or die ("No Database");
    mysql_select_db("phphitscounter", @mysql_connect ("$db_host","$db_username","$db_pass"));

    $ip = $_SERVER['REMOTE_ADDR'];
    $Page = $_SERVER['PHP_SELF'];
    $pagenum = preg_replace("/[^0-9]/", '', $Page);

    $query = mysql_query("SELECT ip FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
    if (mysql_num_rows($query) > 0) {
        echo "You have already voted";
    }
    else{
        echo "Thank you for voring";
        mysql_query("INSERT INTO vote (ip, page, voteup, votedown) VALUES ('$ip', '$pagenum', 0, 0)");
        mysql_query("UPDATE vote SET voteup = voteup + 1 WHERE ip = '$ip' AND page = '$pagenum'");
    }

    $voteup = mysql_query("SELECT voteup FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
    $results_voteup = mysql_fetch_array($voteup, MYSQL_NUM);
    print ($results_voteup[0]);
    echo "1";
}
else if ($contentVar == "con2") {
    $db_host = "host";
    $db_username = "username";
    $db_pass = "pass";
    $db_name = "phphitscounter";

    @mysql_connect ("$db_host","$db_username","$db_pass") or die ("Could Not Connect To The Database");
    @mysql_select_db ("$db_name") or die ("No Database");
    mysql_select_db("phphitscounter", @mysql_connect ("$db_host","$db_username","$db_pass"));

    $ip = $_SERVER['REMOTE_ADDR'];
    $Page = $_SERVER['PHP_SELF'];
    $pagenum = preg_replace("/[^0-9]/", '', $Page);

    $query = mysql_query("SELECT ip FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
    if (mysql_num_rows($query) > 0) {
        echo "You have already voted";
    }
    else {
        echo "Thank you for voring";
        mysql_query("INSERT INTO vote (ip, page, voteup, votedown) VALUES ('$ip', '$pagenum', 0, 0)");
        mysql_query("UPDATE vote SET votedown = votedown + 1 WHERE ip = '$ip' AND page = '$pagenum'");
    }

    $votedown = mysql_query("SELECT voteup FROM vote WHERE ip = '$ip' AND page = '$pagenum'");
    $results_votedown = mysql_fetch_array($votedown, MYSQL_NUM);
    print ($results_votedown[0]);
    echo "2";
} 

test.php 页面:

<html>

<head>
    <script type="text/javascript" src="Scripts/jquery.js"></script>
    <script language="JavaScript" type="text/javascript">
    function swapContent(cv) {
        $("#myDiv").html('<img src="Assets/loading.gif" width="50" height="50"/>').show();
        var url = "scripts/newtest.php";
        $.post(url, {
            contentVar: cv
        }, function(data) {
            $("#myDiv").html(data).show();
        });
    }
    </script>
    <style type="text/css">
    #myDiv {
        width: 200px;
        height: 150px;
        padding: 12px;
        border: #666 1px solid;
        background-color: #FAEEC5;
        font-size: 18px;
    }
    </style>
</head>

<body>
    <img src="Assets/like.png" href="#" onClick="return false" onmousedown="javascript:swapContent('con1');" />
    <img src="Assets/dislike.png" href="#" onClick="return false" onmousedown="javascript:swapContent('con2');" />
    <div id="myDiv"></div>
</body>

</html>
4

2 回答 2

0

替换此代码:

<img src="Assets/like.png" href="#" onClick="return false" onmousedown="javascript:swapContent('con1');"/>
<img src="Assets/dislike.png" href="#" onClick="return false" onmousedown="javascript:swapContent('con2');"/>

和:

<img src="Assets/like.png" href="#" id="up"/>
<img src="Assets/dislike.png" href="#"id="down"/>

<script>
$('#up').mousedown(function() {
swapContent('con1');
});

$('#down').mousedown(function() {
  swapContent('con2');
});
</script>
于 2012-04-06T06:39:02.117 回答
0

做这样的事情

<img src="Assets/like.png" href="#" onClick="swapContent(1);"/>
<img src="Assets/dislike.png" href="#" onClick="swapContent(2);"/>

function swapContent(value){

$("#myDiv").html('<img src="Assets/loading.gif" width="50" height="50"/>').show();
var url = "scripts/newtest.php";
$.post(url, {contentVar: value} ,function(data) {
   $("#myDiv").html(data).show();
});


}
于 2012-04-06T06:47:27.297 回答