Page 1 of 1

AJAX Hitting Enter Problem

Posted: Wed Jun 13, 2007 5:06 pm
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]

Posted: Wed Jun 13, 2007 5:18 pm
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.

Posted: Wed Jun 13, 2007 5:21 pm
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.

Posted: Wed Jun 27, 2007 11:43 pm
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).