如何插入ajax方法,通过按下按钮将用户ID发布到mysql,刷新页面后,按钮上显示相同的文本

时间:2014-08-23 12:57:05

标签: php html ajax

-------------------------HTML code----------------------------
<body class="bdy">

    <?php include "includes/head.php"; ?>




<?php

//include "includes/connection.php";
 $us=$_GET['Uid']; (user id to post)
 echo '<div class="container">
    <button class="btn followButton follow" rel="6">Follow</button>
</div>';


include "includes/uprofile1.php";
include "includes/footer.php";

?>


<script src="style/javascript/follow.js"></script>

</body>

-------------------jquery code-----------------------
$(document).ready(function(){

$('button.followButton').on('click', function(e){
    e.preventDefault();
    $button = $(this);


    if($button.hasClass('following')){

        //$.ajax(); Do Unfollow

//这只是我在添加ajax调用时需要帮助的按钮代码....

        $button.removeClass('following');
        $button.removeClass('unfollow');
        $button.text('Follow');
    } else {
     var data=$(this).attr('Uid');
     $.ajax({
        url:"follow.php",
        type:"post",
        data:data,


     });
    $button.addClass('following');
        $button.text('Following');

    }
});

$('button.followButton').hover(function(){
     $button = $(this);
    if($button.hasClass('following')){
        $button.addClass('unfollow');
        $button.text('Unfollow');
    }
}, function(){
    if($button.hasClass('following')){
        $button.removeClass('unfollow');
        $button.text('Following');
    }
});

    });

请有人帮我插入ajax帖子,将用户ID发布到mysql。我不明白如何进行ajax调用。

1 个答案:

答案 0 :(得分:0)

由于您没有提供任何错误我会猜测..

 $.ajax({
    url:"follow.php",
    type:"post",
    data:data
 });

必须在数据后删除额外的逗号:data