AJAX Hitting Enter Problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

AJAX Hitting Enter Problem

Post by Dale »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Right, my AJAX built "instant messenger" thing I've been building from random snippets of code here, there and everywhere works fine when I press the "Send" button, though when I hit "Enter" on my keyboard it messes up. :/

When I press the "Send" button the URL stays as:
/im.php?timefrom=1181770983&towhom=1&keykey=tabruchi

Though when I press the "Enter" key on my keyboard is goes to:
im.php?

Weird? Yeah.

Here is the code for the function that "adds" the chat to the database:
[syntax="javascript"]function ajaxAddChat(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Hmmm... something messed up. Message the Admin!");
				return false;
			}
		}
	}

	var message = document.getElementById('message').value;
	var theString = "?message=" + message;
	ajaxRequest.open("GET", "add_chat.php" + theString + "&timefrom=<?php print $_GET[timefrom]; ?>&whoto=<?php print $_GET[towhom]; ?>&keykey=<?php print $_GET[keykey]; ?>", true);
	ajaxRequest.send(null);
}
And here is the code for the form-y bit:

Code: Select all

	<tr><form name='myForm' onSubmit='ajaxAddChat()'>
	<td width="100%" bgcolor="#3d3d3d" align="left" style="color:#FFFF00; font-weight:bold; text-decoration:none;">Message: <input type='text' id='message' onsubmit='ajaxAddChat()' size='50' /><input type='button' onclick='ajaxAddChat()' onsubmit='ajaxAddChat()' value='Send' /></td>
	</form></tr>
Notice i've also tried the onsubmit='' command too... which that doesn't work either.

Any ideas?


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
stakes
Forum Commoner
Posts: 48
Joined: Tue Jun 12, 2007 12:05 pm

Post by stakes »

Maybe it's triggering something else than your button when you hit enter. Try making the button an input type=submit instead of input=button.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

stakes wrote:Maybe it's triggering something else than your button when you hit enter. Try making the button an input type=submit instead of input=button.
Doing that makes it mess up when pressing the "Enter" key AND when pressing the "Send" button.
proxor
Forum Newbie
Posts: 1
Joined: Wed Jun 27, 2007 11:34 pm

Post by proxor »

Don't you think you use too many onsubmits? I use something like that:

Code: Select all

<form method="POST" name="chatpost" onsubmit="return false;">
<input type="text" size="98" name="message" onkeypress="if (event.keyCode==13) {post(this.form.message.value); this.form.message.value='';};">
<input type="button" name="send" value="Send" onclick="post(this.form.message.value); this.form.message.value='';">
</form>
and it works perfect.
Okay, event.keyCode is equal to code of the key pressed while cursor in the textbox named "message" and onkeypress raises each time you press key in that textbox. 13 is the code of "Enter". "post" is my func, similar to yours "ajaxAddChat" (but I use POST method to send any params to PHP-script).
Post Reply