2012-02-28 55 views
0

我有麻煩實現jquery到我的像按鈕腳本。有人可以解釋爲什麼。我對編碼相當陌生。謝謝。相關代碼如下。 id指收到的項目的ID。jquery/php像按鈕問題

<script type="text/javascript"> 

$('#fav').click(function(e){ 
$.post('favbuttonchange.php?id=<? echo $id; ?>', 
     function() { 
if($('#fav').hasClass('unfavoritebutton')){ 
     $(this).toggleClass('favoritebutton'); 
    } else { 
     $(this).toggleClass('unfavoritebutton'); 
    } 
    e.preventDefault(e); 
    }); 
}); 

</script> 

<span class="productlike"> 
<?php 

$favquery1=mysql_query("SELECT fav_id FROM favourites WHERE products_products_id='$id' AND products_users_user_id='$user_id'")or die ("Could not select database because ".mysql_error()); 
$favcount=mysql_num_rows($favquery1); 
if ($favcount == 1){ ?> 
    <form class='likefav' > 
    <input id='fav' class='unfavoritebutton' type='submit' name='unfavourite' value=''/> 
    </form> 
<?php } 
elseif($favcount == 0) { ?> 

    <form class='likefav'> 
    <input id='fav' class='favoritebutton' type='submit' name='favourite' value='' /> 
    </form> 
<?php } 
?> 
</span><br /> 

那麼這裏就是我的favbuttonchange.php

$id= (int)strip_tags($_GET['id']); 

$favquery1=mysql_query("SELECT fav_id FROM favourites WHERE products_products_id='$id' AND products_users_user_id='$user_id'")or die ("Could not select database because ".mysql_error()); 
$favcount=mysql_num_rows($favquery1); 

if ($favcount == 0){ 

    $favquery2=mysql_query("INSERT INTO favourites (products_products_id, products_users_user_id) VALUES ('$id', '$user_id')")or die ("Could not select database because ".mysql_error()); 

} 
if($favcount ==1) { 

$favquery3=mysql_query("DELETE FROM favourites WHERE products_products_id = '$id' AND products_users_user_id = '$user_id'"); 

} 
+0

什麼不工作? – ceejayoz 2012-02-28 20:45:44

+0

當我點擊最喜歡/不喜歡按鈕。它會轉到url:product.php?favorite =出於某種原因。沒有任何工作,無論是PHP或jQuery。沒有jQuery,PHP的工作很好壽 – Anonymous 2012-02-28 20:53:39

回答

0

壞synatx。否if聲明。只需撥打$('#fav').hasClass(..)即可設置每個元素。