0

I'm making a simple voter that takes either a "like" vote or "dislike" vote. Then, I count the total number of likes and dislikes and output the total numbers. I figured out how to put in the votes using Jquery Ajax, but the number of votes do not update after I put in a vote. I would like to update the $numlike and $numdislike variables using Jquery Ajax.

Here is the PHP script pertaining to the output:

$like = mysql_query("SELECT * FROM voter WHERE likes = 1 ");
$numlike = 0;
while($row = mysql_fetch_assoc($like)){ 
    $numlike++;
}

$dislike = mysql_query("SELECT * FROM voter WHERE likes = 0 ");
$numdislike = 0;
while($row = mysql_fetch_assoc($dislike)){  
    $numdislike++;
}

echo "$numlike like";
echo "<br>";
echo "$numdislike dislike";

UPDATE: Jquery Ajax for uploading vote

<script>
$(document).ready(function(){
    $("#voter").submit(function() {

    var like     = $('#like').attr('value');
   var dislike   = $('#dislike').attr('value');

        $.ajax({
            type: "POST",
            url: "vote.php",
            data: "like=" + like +"& dislike="+ dislike,   
            success: submitFinished
            });

            function submitFinished( response ) {
  response = $.trim( response );

  if ( response == "success" ) {
        jAlert("Thanks for voting!", "Thank you!");
        }

    return false;
    });
});
</script>

<form id="voter" method="post">
<input type='image' name='like' id='like' value='like' src='like.png'/>
<input type='image' name='dislike' id='dislike' value='dislike' src='dislike.png'/>
</form>

vote.php:

if ($_POST['like'])
{
    $likeqry  = "INSERT INTO test VALUES('','1')";
    mysql_query($likeqry) or die(mysql_error());
    echo "success";
}

if ($_POST['dislike'])
{
    $dislikeqry  = "INSERT INTO test VALUES('','0')";
    mysql_query($dislikeqry) or die(mysql_error());
    echo "success";
}
4
  • 1
    Please tell me that this code is a joke. You are using the most inefficient way of counting rows. Using mysql_num_rows() on the resultset would be inefficient, but your code iterating over every row is worse. Anyway, use SELECT COUNT(*) FROM and then use mysql_result($resultset, 0, 0) to get the row count. Commented Jul 9, 2011 at 9:13
  • ^ SELECT COUNT(*) FROM voter WHERE like=0 would be much better Commented Jul 9, 2011 at 9:17
  • select like, count(*) from voter group by like would even be better: one query to get both the likes and dislikes Commented Jul 9, 2011 at 9:32
  • where is the javascript code that sends the vote and the PHP code that reads it and put it in database? Commented Jul 9, 2011 at 9:34

2 Answers 2

2

If you want to change current like or dislike number after clicking it you must return result instead of printing it ! return json result and echo this and change div innerHTML to see new result !

............
............
............
$dislike = mysql_query("SELECT * FROM voter WHERE likes = 0 ");
$numdislike = 0;
while($row = mysql_fetch_assoc($dislike)){  
    $numdislike++;
}

echo json_encode( array( $numlike, $numdislike ) ) ;
exit();

Now in your html code :

       $.ajax({
            type: "POST",
            url: "vote.php",
            context:$(this)
            data: "like=" + like +"& dislike="+ dislike,   
            success: submitFinished(data)
            });

            function submitFinished( response ) {
  response = $.parseJSON( response );
  //Now change number of like and dilike but i don't know where are shown in your html
  $('#like').attr('value',response[0]);
  $('#dislike').attr('value',response[1]);
  return false;
    });
Sign up to request clarification or add additional context in comments.

5 Comments

I don't have a html section for the output. Can you give an example what it should look like?
Hmmm! I mean where does your like or dislike count show ? div ? input ? Where you display this number to user ? you could change it's value!
In the first PHP code section I posted, I am just using "echo $numlike" and "echo $numdislike" to show the vote count. I am not using any divs to show vote count. How do I show the vote count without submitting a vote?
So What was your problem ? You said that when i update my vote but the number of votes do not update after I put in a vote! So how you figure out that vote not updated ?
For example, when I click the "like" button, the jquery Ajax uploads the "like" vote into the SQL table but on the website itself, I do not see an increase in the number of "like" votes based on "echo $numlike" result
1

You can send a $_GET or $_POST variable to the file that you are calling with AJAX.

.load("google.com", "foo=bar", function(){ 

 });

1 Comment

can you please explain in terms of my sample code? I'm quite new to Jquery and Ajax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.