Jump to content

Page refresh on submit


EchoFool

Recommended Posts

Hey I have some ajax on my site which posts messages to a PHP script but it still refreshes the page when they do so which is frustratin and i don't want it doing that.

 

My code:

 

function sendMessage()
{
     $.ajax({
          type: "POST",
          url: "chatpost.php",
          async: false,
          data: "message="+$("#myMessage").val(),
           success: function(msg){
                refreshChat();
           }
      });
}
function refreshChat()
{
      $.ajax({
          type: "POST",
          url: "chat.php",
           success: function(msg){
                $("#chatArea").html(msg);
           }
      });
}

 

Form:

 


<form onSubmit="sendMessage()" method="POST">
<input type="texT" id="myMessage" class="blank" style="width:74%;">

<input type="button" class="blank" value="Send Message" onclick="sendMessage();">
</form>

 

Any ideas on how to prevent the refresh ?

Link to comment
https://forums.phpfreaks.com/topic/204437-page-refresh-on-submit/
Share on other sites

Thank you for the kind reply,

 

If i may squeeze one final question in,

 

I am using a function call every half a second to load the chat with:

setInterval( "updateShouts()", 500 );

So my question is, is this the best way to load the chat ? Or is there a better method, equally should i store logs in a database or a text file, which is more efficient if were talking about lots of users using the script?

 

Well, having never done something like this before that's the way I'd go about it. Using setInterval. I'd perhaps have AJAX check with a PHP function that only queries the database to check if there are new posts, and not actually retrieve them. That way it would be quicker then retrieving the same posts over and over again and updating the chat even if there is nothing new. Then if there are new posts, fetch the data and update accordingly.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.