如何更新号码而不重新加载

时间:2014-01-02 22:57:44

标签: php jquery

我正在研究jquery的投票系统。我有一个用户可以投票的地方,或者如果他们改变主意投票,它会从upvote中扣除并将其投入到投票中。但我的问题是,当选择投票时我无法刷新这两个数字,所以它只使用原始数字而不是更新的数字。

投票页面

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
    $(".vote").click(function() {
        var id = $(this).attr("id");
        var name = $(this).attr("name");
        var dataString = 'id=' + id;
        var parent = $(this);

        if (name == 'up') {

            $.ajax({
                type: "POST",
                url: "up_vote.php",
                data: dataString,
                cache: false,

                success: function(html) {
                    parent.html(html);
                }
            });
        } else {
            $.ajax({
                type: "POST",
                url: "down_vote.php",
                data: dataString,
                cache: false,

                success: function(html) {
                    parent.html(html);
                }
            });
        }
        return false;
    });
});
</script>
<?php
$sql=mysql_query("SELECT * FROM uploads LIMIT 9");
while($row=mysql_fetch_array($sql))
{
    $msg=$row['title'];
    $mes_id=$row['id'];
    $up=$row['up'];
    $down=$row['down'];
?>
<a href="" class="vote" id="
    <?php echo $mes_id; ?>" name="up">
    <?php echo $up; ?> up
</a>
<div class='down'>
    <a href="" class="vote" id="
        <?php echo $mes_id; ?>" name="down">
        <?php echo $down; ?>
    </a>
</div>
<div class='box2' >
    <?php echo $msg; ?>
</div>undefined</div>undefined
<?php } ?>

up_vote.php页面..

(down_vote.php与up_vote完全相同,只是它只是向下变化。)

<?php
include("config.php");
$ip = $_SERVER['REMOTE_ADDR'];

if ($_POST['id']) {
    $id     = $_POST['id'];
    $id     = mysql_escape_String($id);
    //Verify IP address in Voting_IP table
    $ip_sql = mysql_query("select ip from votes where img_id='$id' and ip='$ip'");
    $count  = mysql_num_rows($ip_sql);

    if ($count == 0) {
        // Update Vote.
        $sql = "UPDATE uploads SET up=up+1 WHERE id='$id'";
        mysql_query($sql);
        // Insert IP address and Message Id in Voting_IP table.
        $sql_in = "insert into votes (id,img_id,ip,type) values ('','$id','$ip','up')";
        mysql_query($sql_in);
    } else {

        //if already voted change it..
        $result = mysql_query("SELECT * FROM votes WHERE img_id='$id' AND ip='$ip'");
        while ($row = mysql_fetch_array($result)) {
            $vote_type = $row['type'];
        }
        if ($vote_type == 'down') {
            $up   = mysql_query("UPDATE uploads SET up=up+1 WHERE id='$id'");
            $down = mysql_query("UPDATE uploads SET down=down-1 WHERE id='$id'");
            $vote = mysql_query("UPDATE votes SET type=up WHERE img_id='$id' AND ip='$ip'");
        }
    }

    $result   = mysql_query("select up from uploads where id='$id'");
    $row      = mysql_fetch_array($result);
    $up_value = $row['up'];
    echo $up_value;
}
?>

1 个答案:

答案 0 :(得分:0)

不是答案,但评论太久了。这个脚本:

while ($row = mysql_fetch_array($result)) {
        $vote_type = $row['type'];
    }    
if ($vote_type == 'down') {
  /* ... */
  }

我认为你现在不需要它。实际上,您只使用最后一次获取的行,而不是所有行。如果这是你的意图(因为只有一个),那么你根本不需要while()。您可以将其更改为:

$row = mysql_fetch_row($result);
$vote_type = $row['type'];
if ($vote_type == 'down') {
  /* ... */
  }

此外,我建议您将代码更改为PDO。它更安全,不推荐使用mysql_ *。