Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tnsmart

macrumors 6502
Original poster
Aug 23, 2008
279
24
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:
Code:
<html>

<! 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>

  <head>
    <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" />
  </head>
 
<body>

<div id="logo"><img src="/ref/Logo.gif" alt="" /></div>

<table width="500px" id="text">
<tbody>
  <tr>
    <td><label for="name">User Name</label></td>
    <td><label for="pass">Password</label></td>
  </tr>
  <tr>
    <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>
  </tr>
</tbody>
</table>

<div id="submit"><input type="button" value="Login" onclick="verify()" /></div>
<div id="message"></div>

</body>
</html>

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.
 
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.

Craigy said:
Also, your verify.js has no way of triggering at first glance.
He has a input button with an onclick event handler at the bottom.
 
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.
 
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.

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.
 
I don't normally use plain HTML (I use .Net) but shouldn't the submit button be <input type="submit">?
 
Last edited:
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.

Put
PHP:
<form name="login" action="verify.php" method="post">
...
</form>
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.
PHP:
<!-- Comments should be properly done like this -->
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.