0

Here is my php function which gets chat message from database

function get_msg(){

   $query_get = "SELECT * FROM chat WHERE chat_id = $user_id";

   $run = $db->query($query_get);

   $messages = Array();


   while ($message = mysqli_fetch_assoc($run)) {

         $msg_string = $message['msg'];

         echo "$msg_string";
        }

}

And here is the JS function in which i want to call the above php function

function register_popup(id, name)
    {

      for(var iii = 0; iii < popups.length; iii++)
      {
           //already registered. Bring it to front.
           if(id == popups[iii])
           {
                Array.remove(popups, iii);

                popups.unshift(id);

                calculate_popups();

                return;

           }
      }

      var msg_string = <?php echo json_encode($msg_string);?>

      var element = '<div class="popup-box chat-popup" id="'+ id +'">';
      element = element + '<div class="popup-head">';
      element = element + '<div class="popup-head-left">'+ name + '</div>';
      element = element + '<div class="popup-head-right"><a href="javascript:close_popup(\''+ id +'\');">&#10005;</a></div>';
      msg();
      element = element + '<div style="clear: both"></div></div>';
      element = element + '<div class="popup-messages">';
      element = element + '<div class="incoming_mesg">

       // **here i want to call the php function**

       get_msg();
</div>';
      element = element + '<div class="outgoing_mesg">Text area for outging message</div></div></div>';


      document.getElementsByTagName("body")[0].innerHTML = document.getElementsByTagName("body")[0].innerHTML + element;


      popups.unshift(id);

      calculate_popups();

    }
Adam
  • 4,445
  • 1
  • 31
  • 49
  • 5
    Possible duplicate of [How can I call PHP functions by JavaScript?](http://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript) – Adam May 16 '17 at 19:20
  • can you pliz look in to my code once thank you – Suresh Sanasam May 16 '17 at 19:29
  • 2
    @Adam is correct. If you click on the link he provided, it will show you an example of how to use AJAX in JS to call a PHP function. Keep in mind that JS is client side and that PHP is server side. – Cagey215 May 16 '17 at 19:55
  • Seconded. You have to use ajax for this. – ADyson May 16 '17 at 19:58

0 Answers0