Have you tried opening the developer tools window in the browser that you're using to see whether you're getting a Javascript error? One thing you probably need to do is
cancel the form submitting. Even on a form without an
action=""
attribute, the form still gets submitted. It's possible your browser is navigating the page before the request can execute.
Here's how you'd prevent the form submission:
document.getElementById('myForm').onsubmit = function (e) {
e.preventDefault();
sendMessage();
}
Also, you don't need to print all of that HTML using
echo
, you could just open and close the PHP tags and have the HTML content print directly:
$session_id = htmlspecialchars($_SESSION['session_id']);
?>
HTML goes here
<?php
} else {