Ajax PHP Follow Script - 数据库中没有任何内容

时间:2016-05-30 16:18:16

标签: javascript php jquery ajax

我最近在ajax上为初学者发现了一个关于ajax的树屋博客http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php我一直在寻找一个跟随脚本,我已经走到了尽头。目前,跟随按钮会按预期淡出,但尚未在数据库中存储任何值。

Profile.php(按照按钮):

for (i = 0; i <= wcnt; i++){ … }

Ajax.js

          <div id="followbtncontainer" class="btncontainer"><a href="#" id="followbtn" class="bigblue">Follow</a></div>

ajax.php

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

我不确定实际的$ following和$ follower值是否导致问题,并且不传递任何数据。任何帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:-1)

尝试更改ajax.js

$(function(){
    $('#followbtn').on('click', function(e){
       e.preventDefault();
       $('#followbtn').fadeOut(300);

       $.ajax({
           url: '../ajax-follow.php',
           ...

url参数:

url: 'ajax-follow.php',

看看它是否会这样运作