我在我的網站有幾個這個腳本的克隆,他們的工作。該功能是爲了使喜歡,遵循等(任何需要參考表來存儲類似,跟隨等)。問題是與我的喜歡功能它運行.post 12次。我的後續腳本具有相同的jQuery代碼,但不這樣做。所以我訴諸檢查我的fave.php/unfave.php文件,但我找不到任何錯誤。它和我的follow.php/unfollow.php文件一樣。任何人都可以發現或意識到這種奇怪的行爲?奇怪的jQuery .post行爲
基本上我得到12條到我的數據庫。爲什麼?
fave.js
$(document).ready(function(){
$(function(){
$(".fvspan").on("click", ".favebtn", function(){
var fvs = $(this).attr('id');
if(fvs)
{
$.ajax({
type: "POST",
url: "/mobile/inc/vdfave.php",
data: "fvs=" + fvs,
cache: false,
success: function(data){
$("span#" + fvs + ".fvspan").html("<img id='" + fvs + "' class='favebtn-un' src='/assets/faved.png' height='50px'>");
}
});
}
else { }
});
});
$(function(){
$(".fvspan").on("click", ".favebtn-un", function(){
var fvs = $(this).attr('id');
if(fvs)
{
$.ajax({
type: "POST",
url: "/mobile/inc/vdunfave.php",
data: "fvs=" + fvs,
cache: false,
success: function(data){
$("span#" + fvs + ".fvspan").html("<img id='" + fvs + "' class='favebtn' src='/assets/fave.png' height='50px'>");
}
});
}
else { }
});
});
});
vdfave.php
<?php
session_start();
//Make an SQL connection
include('/home/bfreak/www/inc/dbc.php');
$myself = $_SESSION['username'];
$ref = $_POST['fvs'];
$date = date("Y-m-d h:i:s");
if (!isset($_SESSION['loggedIn'])) { }
else{$myinfo = mysql_query("SELECT * FROM users WHERE username = '$myself'");}
while($myid1 = mysql_fetch_array($myinfo))
{
$myid = $myid1['id'];
//Insert form data into database with corresponding structure, in respective order, of SQL columns.
$fvd = "INSERT INTO faves (vid, usr, date) VALUES ('$ref', '$myid', '$date')";
if (!mysql_query($fvd, $con)) {die('Fatal Error: ' . mysql_error());}
}
mysql_close($con);
?>
你嵌套了「文檔準備好」。你可以從第一行刪除'$(document).ready(function(){'並且再次檢查嗎? – Azamat
'mysql_'的功能?不...不是... – Terry
我想我用一些邏輯來計算它。 favebtn的onda頁面現在,我必須找出一個解決方案,使$(this)引用該項目的唯一ID –