php-mysql投票系统?

时间:2012-04-06 06:26:46

标签: php javascript jquery mysql ajax

我正在使用以下脚本在我的网站中构建一个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>

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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();
});


}