PHP和AJAX问题?

PHP和AJAX问题?,php,mysql,ajax,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"; //这将输出电影id、新分级、新投票和消息。 e

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

//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";
//这将输出电影id、新分级、新投票和消息。
echo“投票并保存.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";
}
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);
        }
    }
}
if(mysql\u num\u行(mysql\u查询(“从'vorters'中选择*,其中'id`='”、$id.“&&&`ip`='、$ip.”)==0){
//这将插入有关用户的信息,因此他们不能再为同一部电影投票。
mysql_查询(“插入到`投票者'(`id`,`ip`)值('''.$id.'','.$ip.'')中);
//这将增加一张选票,并将评分添加到总评分中。
mysql_query(“更新`movies`SET`voces`=voces+1,`rating`=rating+“$vote_cast.”其中`id`=''.$id.'''))或die(mysql_error());
//这将检索有关电影的最新更新数据。
$data=mysql\u-fetch\u数组(mysql\u查询(“从'movies'中选择*,其中'id`='”。$id.“”));
//这将得到平均评分,并将其四舍五入到小数点后一位。
$rating=round($data['rating']/$data['vows'],1);
$voces=$data['voces'];
//这将把输出类型更改为XML,而不是HTML。
标题('Content-Type:text/xml');
标题('Pragma:no cache');
//有效XML文件中的必需标头
回音“.”n;
//这将输出电影id、新分级、新投票和消息。
echo“投票并保存.n”;
}否则{
////这将把输出类型更改为XML,而不是HTML。
标题('Content-Type:text/xml');
标题('Pragma:no cache');
////有效XML文件中的必需标头
回音“.”n;
//如果他们已经投票,将显示此消息,
回声“你已经投票了,n”;
}
}


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

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";
}
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);
        }
    }
}
功能状态变化率(){
如果(http.readyState==4){
var xmlObj=http.responseXML;
var html=xmlObj.getElementsByTagName('result')。项(0)。firstChild.data;
var id=xmlObj.getElementsByTagName('result')。项(0)。getAttribute(“id”);
var voces=xmlObj.getElementsByTagName(“结果”).item(0.getAttribute(“投票”);
var rating=xmlObj.getElementsByTagName(“结果”).item(0.getAttribute(“评级”);
//之前,您可能已经注意到,如果他们已经投票,我们将投票设置为“-1”,这只是为了提供一种检查脚本返回的简单方法。
如果(投票数!=-1){
//这将通知用户他们所投的票。
document.getElementsByName('output_'+id).item(0).innerHTML=“
”+html; //这将设置一个延迟,使该消息在5000毫秒(5秒)内消失。 setTimeout(“document.getElementsByName('output_“+id+”))。项(0)。innerHTML='';5000); //这会将页面上的评级更新为新的评级。 document.getElementsByName('rating_'+id).item(0).innerHTML=rating; document.getElementsByName('voates_'+id).item(0).innerHTML=投票; }否则{ document.getElementsByName('output_'+id).item(0).innerHTML=“
”+html; setTimeout(“document.getElementsByName('output_“+id+”))。项(0)。innerHTML='';5000); } } }
我认为您的问题在于您调用的是
数据,而不是类似于:

但是,为了测试其他所有功能是否正常工作(如果无法修复),请执行以下操作:

var html = "Sample content";
这样,您就可以查看所有的
getElementsByName
调用是否正常工作

有没有理由不使用jQuery这样的库来让它更易于管理和跨浏览器

jQuery使某些事情变得非常简单,例如,您的ajax调用可能看起来像这样(而不是
newxmlhttprequest
以及与之配套的其他代码行)。
id
vote
是任意的,在PHP中将被引用为
$\u POST['id']
$\u 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
):

$(“#输出"+id).html(“
”+html);//按id选择

希望这有助于进一步解释为什么您可能希望使用jQuery或类似的库来简化代码。。。还有你的生活。

我相信你的问题在于你调用的是
数据,而不是类似于:

但是,为了测试其他所有功能是否正常工作(如果无法修复),请执行以下操作:

var html = "Sample content";
这样,您就可以查看所有的
getElementsByName
调用是否正常工作

有没有理由不使用jQuery这样的库来让它更易于管理和跨浏览器

jQuery使某些事情变得非常简单,例如,您的ajax调用可能看起来像这样(而不是
newxmlhttprequest
以及与之配套的其他代码行)。
id
vote
是任意的,在PHP中将被引用为
$\u POST['id']
$\u 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
):

$(“#输出"+id).html(“
”+html);//按id选择

希望这有助于进一步解释为什么您可能希望使用jQuery或类似的库来简化代码。。。还有你的生活。

@mAdCoDeR你的PHP似乎可以很好地发送XML,但问题可能在于你对PHP页面的JavaScript ajax调用。你能把那个密码寄出去吗?(如果您使用的是jQuery或Prototype,请务必提及)@mAdCoDeR您的PHP似乎可以很好地发送XML,但问题可能是w