How to update a PHP variable without reloading page
Using only PHP you can’t update a variable without reloading the page. PHP is a server side language so it cannot update the DOM without making a request to the server(reloading the page).
To do this kind of functionality I would suggest using ajax calls with javascript or jQuery
Ex: You have a controller in your PHP class that handles if a post is liked or not. (I’am using symfony here but i think you get the point).
- /**
- * @Route("/like", name="like_post")
- */
- public function likeAction(){
- // Your logic you want to achieve here
- return new Response(json_encode(array("liked" => true)));
- }
And on your client side you will make something like this
- $.ajax({
- url: "/like",
- type: "POST",
- data: JSON.stringify({"post_id": data.id}), // This will send post id to controller
- success: function(){
- // If the use liked the post before on element html() you can write "dislike" and viceversa
- $("your_html_element_id_that_you_want_to_update").html("liked");
- }
- });
This is my first answer and I hope I answered it correctly. I also want to apologize if my english is bad.
Cheers,
Artigos semelhantes
- Is there any way in c to change the value of a global variable through a function without passing it to the function?
- Por que removem as teclas dedicadas Home, End, Page Up, Page Down do teclado de um laptop?
- O que é otimização on-page e off-page? Como pode ser benéfica?
- How does the OLA app enable GPS automatically without going to the device's setting page and retrieve its location so fast for an Android application?