Search code examples
javascriptphpclientinteractionserver

Server interact with client and vice versa


I am wondering how I allow the server to interact with the client, and the client to interact with the server with PHP, javascript, or some other way. Like with javascript interacting with a server, so when I have a messaging program, the user does not have to refresh the page to see the message notification.


Solution

  • Your looking to make AJAX requests to the server. This can be done with pure JavaScript or jQuery.

    http://api.jquery.com/jquery.ajax/

    function getMessages(){
        $.ajax({
          url: 'get-messages.php',
          success: function(response){
            //Loop through retrieved  messages to append to DOM.
          }
        });
    }
    
    //Set interval to get new messages every second
    setInterval(getMessages, 1000);