title: How to call function in class PHP from JAVASCRIPT. CHAT
body: <p>i want to integrate process file for chat in class php.</p>
  
  <p>If i leave the process.php file in same folder ( public_html ) with chat.js it's worked correctly. But when i put in my class XHR doesnt updated after time setted.</p>
  
  <p>When i send the message, he stored in my chat.txt, but doesn't display. Some help please..</p>
  
  <p>I have this:</p>
  
  <pre><code>private function handlePostData($page)
  {   
          if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  
              switch ($page)
              {
  
                  case 'process':
                      $this-&gt;getUserFunctions()-&gt;chat_process();
                  break;
              }
          }
  }
  </code></pre>
  
  <p>This is the function from main file.</p>
  
  <p>Next i have this: (this is the function in my class with process.php file)</p>
  
  <pre><code>public function chat_process(){
  $function = $_POST['function'];
  
  $log = array();
  
  switch($function) {
  
       case('getState'):
           if(file_exists('chat.txt')){
             $lines = file('chat.txt');
           }
           $log['state'] = count($lines);
           if($state == $count){
               $log['state'] = $state;
               $log['text'] = false;
  
               }
               else{
                   $text= array();
                   $log['state'] = $state + count($lines) - $state;
                   foreach ($lines as $line_num =&gt; $line)
                     {
                         if($line_num &gt;= $state){
                       $text[] =  $line = str_replace("\n", "", $line);
                         }
  
                      }
                   $log['text'] = $text; 
               }
           break; 
  
       case('update'):
          $state = $_POST['state'];
          if(file_exists('chat.txt')){
             $lines = file('chat.txt');
           }
           $count =  count($lines);
           if($state == $count){
               $log['state'] = $state;
               $log['text'] = false;
  
               }
               else{
                   $text= array();
                   $log['state'] = $state + count($lines) - $state;
                   foreach ($lines as $line_num =&gt; $line)
                     {
                         if($line_num &gt;= $state){
                       $text[] =  $line = str_replace("\n", "", $line);
                         }
  
                      }
                   $log['text'] = $text; 
               }
  
           break;
  
       case('send'):
        $nickname = htmlentities(strip_tags($_POST['nickname']));
           $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
            $message = htmlentities(strip_tags($_POST['message']));
       if(($message) != "\n"){
  
           if(preg_match($reg_exUrl, $message, $url)) {
              $message = preg_replace($reg_exUrl, '&lt;a href="'.$url[0].'" target="_blank"&gt;'.$url[0].'&lt;/a&gt;', $message);
              } 
  
  
           fwrite(fopen('chat.txt', 'a'), "&lt;span&gt;". $nickname . "&lt;/span&gt;" . $message = str_replace("\n", " ", $message) . "\n"); 
  
       }
           break;
  
  }
  
  echo json_encode($log);
  }
  </code></pre>
  
  <p>And my chat.js is like this:</p>
  
  <pre><code>    function Chat () {
      this.update = updateChat;
      this.send = sendChat;
      this.getState = getStateOfChat;
  }
  
  //gets the state of the chat
  function getStateOfChat(){
      if(!instanse){
           instanse = true;
           $.ajax({
                 type: "POST",
                 url: "/?exec=process",
                 data: {  
                          'function': 'getState',
                          'file': file
                          },
                 dataType: "json",
  
                 success: function(data){
                     state = data.state;
                     instanse = false;
                 },
              });
      }    
  }
  
  //Updates the chat
  function updateChat(){
       if(!instanse){
           instanse = true;
           $.ajax({
                 type: "POST",
                 url: "/?exec=process",
                 data: {  
                          'function': 'update',
                          'state': state,
                          'file': file
                          },
                 dataType: "json",
                 success: function(data){
                     if(data.text){
                          for (var i = 0; i &lt; data.text.length; i++) {
                              $('#chat-area').append($("&lt;p&gt;"+ data.text[i] +"&lt;/p&gt;"));
                          }                                 
                     }
                     document.getElementById('chat-area').scrollTop = document.getElementById('chat-area').scrollHeight;
                     instanse = false;
                     state = data.state;
                 },
              });
       }
       else {
           setTimeout(updateChat, 1500);
       }
  }
  
  //send the message
  function sendChat(message, nickname)
  {       
      updateChat();
       $.ajax({
             type: "POST",
             url: "/?exec=process",
             data: {  
                      'function': 'send',
                      'message': message,
                      'nickname': nickname,
                      'file': file
                   },
             dataType: "json",
             success: function(data){
                 updateChat();
             },
          });
  }
  </code></pre>