0
<script>
    $(document).ready(function(){
       $(".add_friend").click(function(){
           f_id = this.id;
           user_id = <?php echo $_SESSION['user_id']; ?>;
           $.ajax({
              type:"POST",
              data:{"f_id":f_id,"user_id":user_id},
              url:"request_send.php",
              success:function(data){
                  $("#add").css("display","none");
                  $(".friend").css("display","block");
              }
           });
       });
    });
</script>

<div class="profile_btnn" id="add" style="display:block;">
    <button type="submit" name="add_friend" id="<?php echo $row_add['user_id']; ?>" class="btnplus add_friend"><i class="fa fa-plus"></i>&nbsp;Add Friend</button>
</div>
<div class="friend" style="display:none;">
    <div class="profile_btnn set_btn">
        <button type="submit" name="req_sent" id="<?php echo $row_req['f_id']; ?>" class="btnplus req_sent"><i class="fa fa-plus"></i>&nbsp;Request Sent</button>
    </div>
</div>

In this code. I want to change div i.e. id="add" with class="friend" after ajax success. Now, What happens when I click on add_friend it works successfully and change id="add" with class="friend". But the problem is when I refresh the page it again show me add friend. So, How can I fix this issue? Please help me.

Thank You

Rohit Sharma
  • 3,304
  • 2
  • 19
  • 34
Rudra
  • 156
  • 1
  • 4
  • 13
  • 2
    JavaScript is client-side, PHP is server side. When you refresh the page you're getting it from the server. If you have criteria you want to use to decide whether or not to display something on initial page load, it has to be in PHP. – miken32 Oct 11 '18 at 05:11
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – miken32 Oct 11 '18 at 05:11
  • 1
    In this case, in PHP you can just check if the friend is already added in the current logged in users list. If it is then don't show add div and show friend div. – CodeThing Oct 11 '18 at 05:11
  • 2
    _Side note:_ Instead of setting the css manually: `$(...).css("display","none/block");`, you can do `$(...).hide();` and `$(...).show()`. (in my opinion, it looks a bit cleaner). – M. Eriksson Oct 11 '18 at 05:15
  • 1
    You need to check in PHP code if there is already a request in the database and accordingly show buttons. – Aditya Sharma Oct 11 '18 at 05:16

0 Answers0