SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    sweden
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    submit form on enter key

    I'm working on a little webchat script the following form:

    <form onSubmit="return doSubmit" name="chatform" method="post" action="chat.php" target="posts">
    <input type="text" size=80 name="chat">
    <input type="hidden" name="nick" value="<?php echo $nick; ?>">
    <input type="button" onClick="doSubmit()" name="Submit" value="Send">
    <input type="hidden" name="action" value="posts">
    </form>

    <script language="JavaScript">
    function doSubmit()
    {
    if(document.chatform.chat.value == '') {
    alert('Text field empty!');
    document.chatform.chat.focus();
    return false;
    }

    document.chatform.submit();
    document.chatform.chat.value = '';
    document.chatform.chat.focus();
    return true;
    }
    </script>

    The problem is that the textbox is not cleared if you submit the form by pressing the enter key. Pressing the Send button clears it though. Both enter and Send button seems to submit the form and the text is displayed in the chat frame. I guess that there is a small thing I'm missing here, please help me find it

  2. #2
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    try this

    You were using a generic button instead of a submit button so the enter key wont work ..... I ve made some minor changes and it works

    Code:
    <form onSubmit="return doSubmit();" name="chatform" method="post" action="chat.php" target="posts">
    <input type="text" size=80 name="chat">
    <input type="hidden" name="nick" value="<?php echo $nick; ?>">
    <input type="submit" value="Send">
    <input type="hidden" name="action" value="posts">
    </form>
    
    <script language="JavaScript">
    function doSubmit() {
    if(document.chatform.chat.value == '') { 
    	alert('Text field empty!');
    	document.chatform.chat.focus();
    	return false;
    	}
    else	{
    	document.chatform.chat.value = '';
    	document.chatform.chat.focus();
    	return true;
    	}
    }
    </script>

  3. #3
    SitePoint Member
    Join Date
    Jan 2005
    Location
    sweden
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it sure does, thanks so very much, you guys are the greatest!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •