title: how to stay the color of the button when hit like and refresh page
body: <p>the default color of my like button is gray and when user hit like button it will run the togglepost "like" and the color will change to red.. i use ajax to insert the data if the user hit like the button... and if the user hit the like button again when its red it will run the togglepost "dislike" and the data from my db will be removed.. </p>
  
  <p><strong>button like</strong></p>
  
  <pre><code>&lt;div class="boxcoracao"&gt;
      &lt;span class="coracao" name="like"&gt;&lt;br&gt;&amp;emsp;&amp;emsp;&amp;emsp;Love&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>jquery function</strong></p>
  
  <pre><code>&lt;script&gt;
  $(".like").click(function(){ 
     $('.boxcoracao .coracao',this).toggleClass("ativo");
     var lpid = $(this).closest("div#buttons").find("#likepid").val();
     var lmid = $(this).closest("div#buttons").find("#likemid").val();
    if ($('.boxcoracao .coracao',this).hasClass("ativo")){  
    // update the text to show what the next click would be
      togglePost("like", lpid, lmid); // run function 
      alert("succes");
    } else {
     // update the text to show what the next click would be
      togglePost("dislike", lpid, lmid); // run function
       alert("failed");
    }
  
  
  
  function togglePost(action,lpid,lmid){
  
    $.ajax({
          type: "post",
          url: "../controller/like_controller.php",
          data: "action="+action+"&amp;postid="+lpid+"&amp;postmember="+lmid,
          success: function(data){
            window.location.reload();
          },
          error: function(e){
           alert("please try again...");
          }
        });
   }
  
  }); 
  &lt;/script&gt;
  </code></pre>
  
  <p>here is my <strong>sql query</strong>....</p>
  
  <pre><code>    function UpdateLikes($postid, $postmember, $likeid, $action){
  
          if ($action == "dislike"){
           $sql = "DELETE FROM plike WHERE pl_puid = '$postid' AND pl_uid = '$likeid'";
              var_dump($sql);
          $result = $this-&gt;dbh-&gt;prepare($sql);
          $result-&gt;execute();
          return $result-&gt;rowCount() ? true : false;
          } else{
          // before inserting you might want to check if they alredy liked or not before adding their count again.
          $query = "INSERT INTO plike SET pl_puid='$postid',pl_memid='$postmember',pl_uid='$likeid'";
              $query = $this-&gt;dbh-&gt;prepare($query);
              var_dump($query);
              $result = ($query-&gt;execute() ? true : false);
              return $result;
          }
  
    }
  </code></pre>
  
  <p>here is the gif button..</p>
  
  <p><a href="https://i.stack.imgur.com/UwJhW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UwJhW.png" alt="enter image description here"></a></p>
  
  <p>but my problem is when a user hit the like button and refresh the page the like button will reset to gray instead of red..any idea on how to solve this?.. when user hit like it will be red and when user refresh the page the button will still be red unless the user hit the button again to unlike?..</p>