Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
<?php
session_start();

if (isset($_SESSION['session_id'])) {
    
    $session_id = htmlspecialchars($_SESSION['session_id']);
    
    printf(" il tuo session ID è %s", $session_id);
    echo "<br>";
    printf("%s", '<a href="logout.php">logout</a>');
	echo "
	  <script>
	  function sendMessage(){
		  
		  var message=document.getElementById('message').value
		  var http = new XMLHttpRequest();
          var url = 'messages.php';
          var params = 'message='+message;
          http.open('POST', url, true);

          http.onreadystatechange = function() {//Call a function when the state changes.
            //if(http.readyState == 4 && http.status == 200) {
            //}
			alert('test');
			alert(http.responseText);
          }
          http.send(params);
		  alert(http.responseText);
	  }
	  document.getElementById('myForm').onsubmit = function() {sendMessage()}
	  </script>
	  <form id='myForm'>
	  <input type='text' id='message'>
	  <input type='submit'>
	  </form>
	";
} else {
    printf("Effettua il %s per accedere all'area riservata", '<a href="../login.html">login</a>');
}


What I have tried:

i've tried to use wireshark to see if the request was sent
Posted
Updated 30-Mar-22 2:12am

1 solution

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:
JavaScript
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:
PHP
    $session_id = htmlspecialchars($_SESSION['session_id']);
?>

HTML goes here

<?php
  } else {
 
Share this answer
 
Comments
Riccardo Miccinilli 30-Mar-22 8:56am    
what do you mean with cancel the form submitting? sorry my lack of knowledge
Chris Copeland 30-Mar-22 9:08am    
What you have is a form with a submit button. When the user clicks the submit button (or probably presses ENTER in the text field) the browser is going to submit that form, which will redirect to a different page (probably the same page but with '?' at the end of the URL). When the browser redirects, it will cancel any Javascript running on the previous page.

As per my example in my answer, you can cancel this browser redirect by calling e.preventDefault(); which tells the browser that you're essentially going to handle things yourself (via Javascript). This should ensure that your Javascript code in the sendMessage() function gets executed completely without interruption.

If it's still not working after my recommended change, you might need to use the developer tools in the browser to look at the console and/or the network requests to see what's happening in the background :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900