Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Submit button issue

Status
Not open for further replies.

irbk

MIS
Joined
Oct 20, 2004
Messages
578
Location
US
So, I have a form, the form was being accidently submitted by enter key presses. So rather then having
Code:
<input type="submit" name="submitform" value="submit">
I changed it to run a little javascript
Code:
<input type="button" name="submitform" value="Submit" onClick="document.mainsubmitform.submit()">
This prevents the form from being submited via an accidential enter key push. However, when the form is submitted, I no longer have the $_POST['submitform'] variable letting me know that the form was actually submited.
So, how do I keep people from accidently submiting the form and maintain some type of variable letting me know when the page was submited?

Thanks in advaince.
 
I tried that. However, when you you get the enter key press I get and error
Code:
Error: Can't move focus to the control because it is invisible, not enabled, or of a type that does nto accept the focus.
So that won't really work for me. Thanks for the suggestion though.
 
I just had a thought, the code you suggested from dynamic drive is
Code:
function handleEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			field.form.elements[i].focus();
			return false;
		} 
		else
		return true;
	}
I don't care if it moves me to the next field, I just want it to not do anything if the enter key is pressed. Is it easy enough to modify this script so that the form just ignores the enter keypress?
 
Should just be able to do this and it will disable the keypress.
Code:
<html>

<head>
<script type="text/javascript">

/***********************************************
* Disable "Enter" key in Form script- By Nurul Fadilah(nurul@REMOVETHISvolmedia.com)
* This notice must stay intact for use
* Visit [URL unfurl="true"]http://www.dynamicdrive.com/[/URL] for full source code
***********************************************/

function handleEnter (field, event) {
var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (keyCode == 13) {
return false;
} 
else
return true;
} 

</script>
</head>

<body>
<form>
<input type="text" onkeypress="return handleEnter(this, event)"><br>
<input type="text" onkeypress="return handleEnter(this, event)"><br>
<textarea>Some text</textarea>
</form>
</body>
</html>
 
Yep, I just figured that out myself and was just about to post it. Thanks!
 
I personally liked the fact that the enter did something the script seems kind of bloated. I'll see if I can rewrite it to make it shorter.
 
If you don't like the functionality of the dd script you can try something like this.
Code:
<html>
	<body>
<script type="text/javascript">
function noEnter(e)
{
	if(e.keyCode == 13 || e.which == 13){
		return false;
	}
	else{ 
		return true;
	}
}
</script>
<form>
	<input type="text" onkeypress=" return noEnter(event)" />
</form>
</html>
[code]
 
I liked the idea that it moved to the next field, however, on my form it had issues and belched errors on some fields. So, rather then trying to solve the problem AND be fancy, I'm completely happy with just solving the problem.
Your script does seem a bit less bloated then what I have now, May have to use it... Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top