...

View Full Version : Forms help



Taipan
03-18-2004, 11:18 PM
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.

firepages
03-18-2004, 11:31 PM
not a PHP question , moving to ....errr probably javascript.

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


<html>
<head>
<title></title>
<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) {
nextField.focus();
}
return false;
}
function validateForm (e, form) {
if (!e || !form) {
return false;
}
// ... client-side validation ...
// form.submit() doesn't call the form's onsubmit handler
form.submit();
// just for clarity
return false;
}
</script>
</head>
<body>
<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);" />
</form>
</body>
</html>

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, 02: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:
http://www.codingforums.com/showthread.php?s=&threadid=30039



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum