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
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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.