我最近在ajax上爲初學者發現了一個樹屋博客http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php我一直在尋找一段時間的關注腳本,並且我已經陷入死衚衕。目前,按鈕按鈕應該會消失,但到目前爲止還沒有值存儲在數據庫中。Ajax PHP關注腳本 - 沒有存儲在數據庫中
Profile.php(遵循按鈕):
<div id="followbtncontainer" class="btncontainer"><a href="#" id="followbtn" class="bigblue">Follow</a></div>
Ajax.js
$(function(){
$('#followbtn').on('click', function(e){
e.preventDefault();
$('#followbtn').fadeOut(300);
$.ajax({
url: '../ajax-follow.php',
type: 'post',
data: {'action': 'follow'},
success: function(data, status) {
if(data == "ok") {
$('#followbtncontainer').html('<p><em>Following!</em></p>');
var numfollowers = parseInt($('#followercnt').html()) + 1;
$('#followercnt').html(numfollowers);
}
},
error: function(xhr, desc, err) {
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
}); // end ajax call
});
$('body').on('click', '#morefllwrs', function(e){
e.preventDefault();
var container = $('#loadmorefollowers');
$(container).html('<img src="images/loader.gif">');
var newhtml = '';
$.ajax({
url: 'ajax-followers.php',
type: 'post',
data: {'page': $(this).attr('href')},
cache: false,
success: function(json) {
$.each(json, function(i, item) {
if(typeof item == 'object') {
newhtml += '<div class="user"> <a href="#" class="clearfix"> <img src="'+item.profile_pic+'" class="avi"> <h4>'+item.username+'</h4></a></div>';
}
else {
return false;
}
}) // end $.each() loop
if(json.nextpage != 'end') {
// if the nextpage is any other value other than end, we add the next page link
$(container).html('<a href="'+json.nextpage+'" id="morefllwrs" class="bigblue thinblue">Load more followers</a>');
} else {
$(container).html('<p></p>');
}
$('#followers').append(newhtml);
},
error: function(xhr, desc, err) {
console.log(xhr + "\n" + err);
}
}); // end ajax call
});
});
ajax.php
<?php require 'database.php' //<?php include 'session-check-index.php' ?>
<?php include 'authentication.php' ?>
<?php
session_start();
$follower=$_SESSION['id'];
$sql = "SELECT * FROM users WHERE username='$username'";
$result = mysqli_query($database,$sql);
$rws = mysqli_fetch_array($result);
$following=$rws['id'];
/**
* this script will auto-follow the user and update their followers count
* check out your POST data with var_dump($_POST)
**/
if($_POST['action'] == "follow") {
$sql=" INSERT INTO `user_follow` (`follower`, `following`, `subscribed`) VALUES ('$follower', '$following', CURRENT_TIMESTAMP);"
/**
* we can pass any action like block, follow, unfollow, send PM....
* if we get a 'follow' action then we could take the user ID and create a SQL command
* but with no database, we can simply assume the follow action has been completed and return 'ok'
**/
mysqli_query($database,$sql) or die(mysqli_error($database));
}
?>
我不知道,如果實際$以下和$ follower值導致問題,並且不傳遞任何數據。任何幫助將不勝感激,謝謝!
你只在發送'page'參數你POST請求,我不能看到一個'行動'參數 – messerbill
其中'$ .ajax()'回調是射擊後調用? '成功'或'錯誤'?在你的'success'回調函數中,你正在運行下面的代碼:'if(data ==「ok」){'但是你沒有在響應中發回OK,所以這個條件永遠不會滿足。但是最後一部分不會影響數據是否插入數據庫。僅供參考 – Marcus
在'ajax.php'中的'SELECT'查詢中,您從哪裏獲取'$ username'?它被設置了嗎?既然你的'INSERT'完全依賴於你的'SELECT'成功了,並且返回'$ following'的值,你應該真的在這兩者中添加一個排序約束。 – Marcus