HTML form won't submit when pressing enter

Discussion in 'Web Design and Development' started by tnsmart, Nov 16, 2010.

  1. tnsmart macrumors 6502

    Aug 23, 2008
    I am trying to get my HTML form (a login page) to submit when pressing enter in the last field, but I don't know how to do it. The submit button works fine, but I want to be able to submit by pressing enter too.

    Here's my code:
    <! This page is the main index page and serves as the login page for all users>
    <! This page is referenced by all pages as the default redirect>
    <! This page references /verify.php through /verify.js>
        <meta http-equiv="refresh" content="120" > 
        <script src="verify.js"></script>
        <title>CASA Login</title>
        <link rel="stylesheet" type="text/css" href="/ref/main.css" />
        <link rel="stylesheet" type="text/css" href="/ref/maincolor.css" />
    <div id="logo"><img src="/ref/Logo.gif" alt="" /></div>
    <table width="500px" id="text">
        <td><label for="name">User Name</label></td>
        <td><label for="pass">Password</label></td>
        <td><input type="text" size="12" name="name" id="name" maxlength="8"/></td>
        <td><input type="password" size="12" name="pass" id="pass" maxlength="8"/></td>
    <div id="submit"><input type="button" value="Login" onclick="verify()" /></div>
    <div id="message"></div>
    I've done some googleing but most people seem to be asking how to disable the function rather than enable it. If it makes a difference, I'm running the latest version of Firefox. Any help is appreciated.
  2. Craigy macrumors 6502

    Jan 14, 2003
    New Zealand
    You do not have a HTML form on the page. Your imputs must sit within a <form>....</form> tag with a valid method and action.

    See here:

    Also, your verify.js has no way of triggering at first glance.
  3. angelwatt Moderator emeritus


    Aug 16, 2005
    Yea, you'll either need to add a form tag around your form like suggested, or your JavaScript needs to listen for keystrokes and check for the enter key. The form wouldn't work for those with JavaScript disabled though.

    He has a input button with an onclick event handler at the bottom.
  4. tnsmart thread starter macrumors 6502

    Aug 23, 2008
    Thanks for the replies. Is it possible to do it without javascript and just <form> and </form>? If so, I'm not sure where to put the tags. Thanks again.
  5. Craigy macrumors 6502

    Jan 14, 2003
    New Zealand
    You'll need to know the follow before you can add the <form> tag

    - What is the name of the page that is handling the login request?
    - Is it expecting a HTTP POST or GET with the details?

    The W3C page in the link I sent over has some easy examples to follow but the main things are the form action (The name if the page that's handling the request) and the form method - either POST or GET.
  6. Nermal, Nov 16, 2010
    Last edited: Nov 16, 2010

    Nermal Moderator


    Staff Member

    Dec 7, 2002
    New Zealand
    I don't normally use plain HTML (I use .Net) but shouldn't the submit button be <input type="submit">?
  7. Darth.Titan macrumors 68030


    Oct 31, 2007
    Austin, TX
    <form name="login" action="verify.php" method="post">
    around your <table></table> tags. Also as Nermal suggests, you should use a submit form element for the form istead of a button. Your onclick handler can still be used.

    While we're at it, those aren't valid comment tags at the beginning either.
    <!-- Comments should be properly done like this -->

Share This Page