0

i am new in php and i am trying to make facebook like chat application and trying to insert text through textarea and show it inside chatbox. it is simple to do this with php but in this situation i don't know how to marge javascript with php code.

here is my javascript code which consist on register popup function.

function register_popup(id, name,msg)
{
    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 element = '<div class="popup-box chat-popup"  id="'+ id +'">';
    element = element + '<div class="msg_head" >';
    element = element + '<div class="popup-head-left" class="close">'+ name +'</div>';
    element = element + '<div class="popup-head-right"><a href="javascript:close_popup(\''+ id +'\');">&#10005;</a></div>';

    element = element + '<div style="clear: both"></div></div><div class="msg_wrap" class="msg_body">'+ msg +'<div class="msg_push"><div class="msg_footer"><textarea class="msg_input" rows="2"></textarea></div></div></div></div>';

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

    popups.unshift(id);

    calculate_popups();

}

here is my mysql query for inserting text

public function addMessage($userId, $message) {
    $addResult = false;

    $cUserId = (int) $userId;
    // Escape the message with mysqli real escape
    $cMessage = $this->dbConnection->real_escape_string($message);

    $query = <<<QUERY
      INSERT INTO `chat`(`user_id`, `message`, `sent_on`)
      VALUES ({$cUserId}, '{$cMessage}', UNIX_TIMESTAMP())
QUERY;

    $result = $this->dbConnection->query($query);

    if ($result !== false) {
        // Get the last inserted row id.
        $addResult = $this->dbConnection->insert_id;
    } else {
        echo $this->dbConnection->error;
    }

    return $addResult;
}
Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
  • 2
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Enstage Jun 14 '17 at 04:27
  • You can use Ajax call – jjaros Jun 14 '17 at 05:28
  • kindly solve this problem for me if it is possible for you – syed Fakhir Jun 14 '17 at 05:31
  • There is no simple solution to this by "merging javascript and php" code, it doesn't work that way, see the document I linked. – Enstage Jun 14 '17 at 08:57

0 Answers0