PHP和AJAX问题?

时间:2009-12-27 05:08:12

标签: php mysql ajax

当用户投票时,脚本会更新我的数据库,但它不会在下面显示以下代码,告诉用户其投票已被排除。

//This will output the movie id, new rating, new votes, and a message.
echo "<result id='".$id."' rating='".$rating."' votes='".$votes."'>Vote cast and saved.</result>n";


如何解决此问题,以便在用户输入她或他的投票时显示上述代码?


下面是我认为是问题的代码的一部分。

if(mysql_num_rows(mysql_query("SELECT * FROM `voters` WHERE `id`='".$id."' && `ip`='".$ip."'")) == 0) {
    //This will insert the information about the user, so they can't vote for the same movie again.
    mysql_query("INSERT INTO `voters`(`id`, `ip`) VALUES('".$id."', '".$ip."')");
    //This will add one more vote and add the rating to the total rating.
    mysql_query("UPDATE `movies` SET `votes`=votes+1, `rating`=rating+".$vote_cast." WHERE `id`='".$id."'") or die(mysql_error());

    //This will retrieve the newly updated data about the movie.
    $data = mysql_fetch_array(mysql_query("SELECT * FROM `movies` WHERE `id`='".$id."'"));
    //This will get the average rating and round it to one decimal place.
    $rating = round($data['rating']/$data['votes'], 1);
    $votes = $data['votes'];

    //This will change the output type to XML, instead of HTML.
    header('Content-Type: text/xml');
    header('Pragma: no-cache');
    //Required header in valid XML files
    echo '<?xml version="1.0" encoding="UTF-8"?>'."n";
    //This will output the movie id, new rating, new votes, and a message.
    echo "<result id='".$id."' rating='".$rating."' votes='".$votes."'>Vote cast and saved.</result>n";
}else{
    ////This will change the output type to XML, instead of HTML.
    header('Content-Type: text/xml');
    header('Pragma: no-cache');
    ////Required header in valid XML files
    echo '<?xml version="1.0" encoding="UTF-8"?>'."n";
    //This message will be shown if they have already voted,
    echo "<result id='".$id."' rating='-1' votes='-1'>You have already voted.</result>n";
}

}


好吧,也许下面这部分我的Ajax代码给了我这个问题。

function statechange_rate() {
    if (http.readyState == 4) {
        var xmlObj = http.responseXML;
        var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data;
        var id = xmlObj.getElementsByTagName('result').item(0).getAttribute("id");
        var votes = xmlObj.getElementsByTagName('result').item(0).getAttribute("votes");
        var rating = xmlObj.getElementsByTagName('result').item(0).getAttribute("rating");
        //Before, you may have noticed we set votes="-1" if they had already voted, this was just to provide an easy way to check the return of our script.
        if(votes != -1) {
            //This will inform the user about the vote they have cast.
            document.getElementsByName('output_' + id).item(0).innerHTML = "<br />" + html;
            //This will set a delay to make that message go away in 5000 miliseconds (5 seconds).
            window.setTimeout("document.getElementsByName('output_" + id + "').item(0).innerHTML = '';", 5000);
            //This will update the rating on the page to the new one.
            document.getElementsByName('rating_' + id).item(0).innerHTML = rating;
            document.getElementsByName('votes_' + id).item(0).innerHTML = votes;
        }else{
            document.getElementsByName('output_' + id).item(0).innerHTML = "<br />" + html;
            window.setTimeout("document.getElementsByName('output_" + id + "').item(0).innerHTML = '';", 5000);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我认为您的问题是,您拨打data而不是像textContent那样:

var html = xmlObj.getElementsByTagName('result').item(0).firstChild.textContent;

但是,只是为了测试其他所有内容是否正常工作(如果没有解决),请执行以下操作:

var html = "Sample content";

通过这种方式,您可以查看所有getElementsByName来电是否正常运行。

你有没有理由不使用像jQuery这样的库来使这个更易于管理和跨浏览器证明?

jQuery使某些事情变得非常简单,例如你的ajax调用看起来像这样(而不是new XMLHTTPRequest和其他代码行)。 idvote是任意的,将在PHP中引用为$_POST['id']$_POST['vote']

$.post('/path/to/file.php', {id: "1", vote:5 }, function(data){
    // Runs when ajax call successfully returns. data is the xml
}, "xml");

选择和更新元素(这将替换您的getElementsByName):

$("#output_" + id).html("<br />" + html); // Select by id

希望这有助于解释为什么您可能希望使用jQuery或类似的库来简化代码......以及您的生活。