开发者

how to update PHP variable with Jquery Ajax?

开发者 https://www.devze.com 2023-03-18 05:40 出处:网络
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 vot

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="po开发者_运维技巧st">
<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";
}


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;
    });


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

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

 });
0

精彩评论

暂无评论...
验证码 换一张
取 消