View Full Version : Forms help

03-19-2004, 12:18 AM
I have a php page that takes input from a form and adds it to a mysql database. At the moment if you type into one of the input test boxes and press "Enter" it submits the form. How can I stop this happening.

Is there some way of making focus go to the next box when "Enter" is pressed?

Any hints, tips would be appreciated.

03-19-2004, 12:31 AM
not a PHP question , moving to ....errr probably javascript.

03-19-2004, 03:25 PM
There are more elegant ways of doing this, of course, but this works:

<script type="text/javascript">
function handleKeyPress (e, nextField) {
if (((e && e.which) ? e.which : window.event.keyCode) != 13) {
// keyCode for enter is 13 - all other keys can pass right on by
return true;
if (nextField) {
return false;
function validateForm (e, form) {
if (!e || !form) {
return false;
// ... client-side validation ...
// form.submit() doesn't call the form's onsubmit handler
// just for clarity
return false;
<form onsubmit="return false;">
<input type="text" name="field1" onkeypress="return handleKeyPress(event, this.form.elements['field2']);" />
<input type="text" name="field2" onkeypress="return handleKeyPress(event, this.form.elements['field3']);" />
<input type="text" name="field3" onkeypress="return handleKeyPress(event, this.form.elements['field4']);" />
<input type="text" name="field4" onkeypress="return handleKeyPress(event, this.form.elements['field5']);" />
<input type="text" name="field5" onkeypress="return handleKeyPress(event, this.form.elements['field6']);" />
<input type="text" name="field6" onkeypress="return handleKeyPress(event, this.form.elements['field1']);" />
<input type="submit" onclick="return validateForm(event, this.form);" />

The important parts: you've got to return false on submit, and specify which field you want to focus on. Put your validation script in where the comment is, and you're good to go.

Roy Sinclair
03-19-2004, 03:25 PM
It is HIGHLY recommended that you do not alter the normal command interface that users have come to expect.

The answer however is here: