Jump to content

php and jquery button


Glenskie

Recommended Posts

I have this button for my like system and for some reason i have to refresh after i click it again... i would like it to be that when i click it it changes the class and i can click it again and do the other function... please help thank you... 

 

 

JQUERY:

<script type="text/javascript">
$(document).ready(function(){
 $('[name="like"]').on("click", function(){
  if($(this).attr('title')=='like'){
   $.post('/like.php',{imid:$(this).attr('id'),action:'like'});
    $(this).removeClass('like').addClass('unlike');
    $(this).attr('title','unlike');
    $(this).attr('name','unlike');
  }
});
    $('[name="unlike"]').on("click", function(){
   if($(this).attr('title')=='unlike'){
    $.post('/like.php',{imid:$(this).attr('id'),action:'unlike'});
     $(this).removeClass('unlike').addClass('like');
     $(this).attr('title','like');
     $(this).attr('name','like');
    }
  });
});
</script>

PHP:

$likes = mysql_query("SELECT lid FROM likes WHERE uid='$id' AND imid='$photoid'");
if(mysql_num_rows($likes)==0){
$likethis = '<div class="like" title="like" id="'.$photoid.'" name="like"></div>';
}else{
$unlikethis = '<div class="unlike" title="unlike" id="'.$photoid.'" name="unlike"></div>';
}

 

Link to comment
Share on other sites

That [name=like] selector you have only applies when adding the event handler. Changing the name won't suddenly make the other handler be applied to it.

 

Use just one handler for both like and unlike, and use the class/title/name to decide what action to take.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.