EchoFool Posted June 10, 2010 Share Posted June 10, 2010 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 ? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted June 11, 2010 Share Posted June 11, 2010 Of course it will, just remove the form tag, because you don't really need it. Or this should do the trick: <form onSubmit="javascript: sendMessage(); return false;" method="POST"> Quote Link to comment Share on other sites More sharing options...
EchoFool Posted June 11, 2010 Author Share Posted June 11, 2010 Okay that did the trick how ever it does not clear the text in the input box when submit occurs - is there a way to clear afterwards? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted June 11, 2010 Share Posted June 11, 2010 You could probably answer that one yourself. When you call refreshChat(), just set the message box to nothing. $("#myMessage").val(""); Quote Link to comment Share on other sites More sharing options...
EchoFool Posted June 11, 2010 Author Share Posted June 11, 2010 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? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted June 11, 2010 Share Posted June 11, 2010 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. Quote Link to comment Share on other sites More sharing options...
EchoFool Posted June 11, 2010 Author Share Posted June 11, 2010 Hmm how would php return a value to tell the script not to call to update chat as thats stretching my ability a bit xD? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.