Hello,

Somewhere in Internet I found a free script which I use in instant messaging system to send messages without page refreshing.

This script works well for 99% of users but from time to time I get messages from users who can't send messages because nothing happends when they click Submit button. This problem appears in Internet Explorer, even 8 version and in mobile phones.

Any ideas what can cause this problem?

Thanks.

<script type="text/javascript">                                         
$(document).ready(function(){
$("#sendmessage").submit(function(){
var str = $(this).serialize();
$.ajax({
type: "POST",
url: "../send.php",
data: str,
success: function(msg){
$("#note").ajaxComplete(function(event, request, settings){
if(msg == "OK")
{
result = '<div class="ok">Message sent</div>';
$("#fields").hide();
$(this).html(result);
}
else
{
result = msg;
$(this).html(result);
}
});
}
});
return false;
});
});
</script>

form:

<div id="note" class="note"><!--note--></div>
<div class="hold">
<div id="fields">
<form id="sendmessage" name="sendmessage">
<textarea name="message" rows="5" cols="30"></textarea>
<input class="button" type="submit" name="submit" value="Send" />
</div></div>

As far as I can see, there's nothing unusual in the javascript. I guess one of three things is happening. In each case, you can't fix it but you can politely advise the user of why things aren't working.

1. Javascript not included or disabled in the user's device
Solution: Insert a <noscript>message</noscript> in the HTML to advise users that the messenger won't work on their device. WebTV is certainly one class of devices highly likely, in my experience, to give problems WebTV's javascript is a strangled version of what exists in browsers. (Google "HTML noscript" for more info).

2. No internet connection
Solution: Include a timeout: ..., in the $.ajax options, and a complete: ... function, in which you test for textStatus=="timeout" and provide an appropriate onscreen message. (see jQuery API)

3. Ajax error occurs
Solution: In the complete: ... function, also test for textStatus=="error" and provide an appropriate onscreen message. (see jQuery API)

Airshow

Looking at your code again, $("#note").ajaxComplete inside the ajax success function looks a bit odd to me in that it is re-established on every occasion success fires. This seems to negate the beneift of putting an ajaxComplete listener in place.

There may be some reason I'm not aware of for doing it that way, but the following seems more logical and economical:

$(document).ready(function(){
	$fields = ;//find once, use many times.
	$send_url = "../send.php";
	$("#sendmessage").submit(function(){
		$.ajax({
			type: "POST",
			url: $send_url,
			data: $(this).serialize()
		});
		return false;
	});
	$("#note").ajaxComplete(function(event, request, settings){
		if (settings.url == $send_url) {
			if(request.responseHTML == "OK") {
				$fields.hide();
				$(this).html('<div class="ok">Message sent</div>');
			}
			else {
				$(this).html(request.responseHTML);
			}
		}
	});
});

This is off-the-cuff, so please test rigorously before using.

The reported problem may or may not be affected.

Airshow

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.