Вопрос

I have a simple (or so I thought) PHP, MYSQL, AXAX system set up to register votes for items on my website. I'm only doing upVotes so there's only one button.

My issue is that when the page loads, the correct number of votes for the item is displayed, but upon the first click of the upVote button, the value does not change. I know I must be missing something, but I have no idea what. It's probably simple... but I just can't figure it out.

Everything connects to the DB just fine. That's why I left all that code out.

Any help is greatly appreciated.

This is the javascript I have:

  <script type="text/javascript">
    function voteButton(str)
    {
    if (str=="")
      {
      document.getElementById("voteUp").innerHTML="";
      return;
      } 
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("voteUp").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","vote?id="+str,true);
    xmlhttp.send();
    }
</script>

This is the php file used to query the database:

<?php
$id=$_GET["id"];

include("myDatabaseStuff.php");
include("global.php");

$result = mysql_query("UPDATE TABLE SET up_mod = up_mod + 1 WHERE img_id = $id");
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

while($row = mysql_fetch_array($getUpVoteResult))
  {
  echo $row['up_mod'];
  }
?>

This is the HTML I use on the page to display the values/button:

<div class="span1" style="margin:30px 0 0 10px;">
    <button onclick="voteButton('<?php echo $id;?>')" class="btn btn-large" type="button" style="margin-top:5px"><div id="voteUp"><?php echo $votes[0] ?></div><i class="icon-thumbs-up"></i></button>
</div>
Это было полезно?

Решение

Add:

$getUpVoteResult = mysql_query("SELECT up_mod FROM table WHERE img_id = $id") or die ("Invalid query: " . mysql_error());

before the while loop.

You also don't need a while loop, since there's just one row.

Другие советы

After your update, please do the separate SELECT.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top