Click to See Complete Forum and Search --> : Form submission


daveWho
04-15-2003, 11:18 AM
This was posted in HTML. It was suggested that this might be the more appropriate forum. Apologies for x-post.

On a form, if someone hits the "Enter" key after filling in a text field then the form gets submitted. Is there any way to prevent the "Enter" key from submitting (except where the submit button has the focus)?

I have tried various combinations of onClick and onKeypress, manipulating this.value, and also the value of a hidden field.

All to no avail.

Anybody able to help?

Vladdy
04-15-2003, 11:25 AM
The right way of approaching the problem is to use form's onsubmit event to verify that there are no empty fields in the form (at least none that you care about). As you parse through the form elements and you encounter an empty field you can just set focus to it, thus from the user prospective hitting enter key at any text field (but last) will just bring them to the next one as if they hit "Tab".
Any attempts to disable "Enter" key will make your form inaccessible to those who do not use mouse.

daveWho
04-15-2003, 12:04 PM
Originally posted by Vladdy
The right way of approaching the problem is to use form's onsubmit event to verify that there are no empty fields in the form (at least none that you care about). As you parse through the form elements and you encounter an empty field you can just set focus to it, thus from the user prospective hitting enter key at any text field (but last) will just bring them to the next one as if they hit "Tab".
Any attempts to disable "Enter" key will make your form inaccessible to those who do not use mouse.



Alas, Any and all fields may be left blank.
Don't necessarily want to disable Enter, just detect which field has focus when they hit it.

Vladdy
04-15-2003, 02:06 PM
Originally posted by daveWho
... all fields may be left blank.
Then why do you need such form in the first place :confused: :D :D :D

DrDaMour
04-15-2003, 09:36 PM
i found that instead of using the type=submit input, just do some other object, like a type=button value="submit" onclick="formname.submit()" & onpress="formname.submit()", or whatever teh pressevent is maybe it's key down, i forget, sorry... Anyways, i've found (quite by accident) this makes it so that enter anywhere wont' submit teh form.

daveWho
04-16-2003, 08:15 AM
Originally posted by Vladdy
Then why do you need such form in the first place :confused: :D :D :D

It is a survey, of approx 300 questions. I'm not going to refuse to accept 299 of of them because someone missed a check-box in Question 173a part iv. I'll take what I can get and be grateful.

DrDaMour
04-16-2003, 09:22 AM
he wasn't saying that's what you should do, verify everythign is right, but the scripts taht verify everything is right make it so teh form isn't submitted simple when an enter key is pressed. so they have the same end result, you'd have to change how they get there.

Vladdy
04-16-2003, 09:59 AM
Originally posted by daveWho
It is a survey, of approx 300 questions. I'm not going to refuse to accept 299 of of them because someone missed a check-box in Question 173a part iv. I'll take what I can get and be grateful.
Hold on a second. Your initial question was about someone hitting "Enter" after filling a TEXT field. How many text fields are there in your 300 questions survey, 299? (we established that there is one checkbox :D :D ). It takes a very bored or altrustic person to complete such survey.
If the person gets nothing out of this survey and your attitude is "take what I can get" why worry about someone submitting it prematurely at all :confused: :confused:
If the person is interested in completing the survey (like IQ test or whatever) there should be nothing wrong with prompting them about a missed field.

daveWho
04-16-2003, 11:20 AM
Originally posted by Vladdy
Hold on a second. Your initial question was about someone hitting "Enter" after filling a TEXT field. How many text fields are there in your 300 questions survey, 299? (we established that there is one checkbox :D :D ). It takes a very bored or altrustic person to complete such survey.
If the person gets nothing out of this survey and your attitude is "take what I can get" why worry about someone submitting it prematurely at all :confused: :confused:
If the person is interested in completing the survey (like IQ test or whatever) there should be nothing wrong with prompting them about a missed field.

Vladdy, the form consists of several multi-choice questions, radio or checkbox. Where one of the choices is "Other" then it is followed by "If other, please specify" in a type=text. There are also name, address, email etc boxes. If someone wants to remain anonymous, it's his right to do so.
My attitude of "take what I can get, and be grateful" doesn't imply that I want to frustrate or confuse the user, and if the form submits before he clearly expects it to then I believe that I have failed in that objective.
And my original question applies whether there are 2 or 200 questions.
I admit I'm stuck, please let's find a technical solution to my question rather than arguing methodologies.

Oh yes, those filling in the survey receive the results of the survey, which has value to them.

daveWho
04-16-2003, 11:22 AM
Originally posted by DrDaMour
i found that instead of using the type=submit input, just do some other object, like a type=button value="submit" onclick="formname.submit()" & onpress="formname.submit()", or whatever teh pressevent is maybe it's key down, i forget, sorry... Anyways, i've found (quite by accident) this makes it so that enter anywhere wont' submit teh form.

Thanks, I tried this, but alas the form still submitted from hitting "Enter" when imput type=text had focus.

DrDaMour
04-16-2003, 12:09 PM
On a form, if someone hits the "Enter" key after filling in a text field then the form gets submitted. Is there any way to prevent the "Enter" key from submitting (except where the submit button has the focus)?


that IS exactly what you just asked for, isnt' it?

daveWho
04-16-2003, 12:27 PM
Originally posted by DrDaMour
that IS exactly what you just asked for, isnt' it?

No, that is exactly waht I'm trying to prevent happening. What I need is that the form submits ONLY when the Submit button is clicked, or when it is tabbed to and "Enter" is hit. Using type="button" with onClick and onKeypress events doesn't prevent the form submitting whenever any type="input" field is tabbed to, and the user hits "enter".

DrDaMour
04-16-2003, 12:32 PM
ok you need to clarify what you want because you are making NO SENSE.

you said:
What I need is that the form submits ONLY when the Submit button is clicked, or when it is tabbed to and "Enter" is hit

and then you said with this set up
Thanks, I tried this, but alas the form still submitted from hitting "Enter" when input type=text had focus.

it did what you wanted!

focus is when it's is tabbed to, right?

DrDaMour
04-16-2003, 12:42 PM
I just thought of what your problem is, you're including the <input type=button onclick=""> thing within the form aren't you! Sorry for not clarifying that at the begiing, but that <input> tag has to be AFTER the </form> close form tag. the see an example quick


<html>



<body>



<form id="tag" action=tag.cgi method=get target=yes topmargin=0>


<input type=text name=name value="" size="10" >

<input type=text name=email value="" size="10" >
<input type=text name=message value="" size="30" >

</form>

<input type=submit value=¡TAG! onClick="tag.submit()">


</body>
</html>

Nedals
04-16-2003, 01:05 PM
I think I understand your problem. When a user enters text in any text box and hits return, your form submits (which you don't want)

Your form probably looks something like this..
<form action="newpage.html">
<input type=text>
...
<input type=submit>
</form>

Change to this (alluded to above) and your problem is solved
<form action="newpage.html">
<input type=text>
....
<input type=button onClick="document.forms[0].submit()">
</form>

daveWho
04-16-2003, 01:05 PM
Originally posted by DrDaMour
I just thought of what your problem is, you're including the <input type=button onclick=""> thing within the form aren't you! Sorry for not clarifying that at the begiing, but that <input> tag has to be AFTER the </form> close form tag. the see an example quick


<html>



<body>



<form id="tag" action=tag.cgi method=get target=yes topmargin=0>


<input type=text name=name value="" size="10" >

<input type=text name=email value="" size="10" >
<input type=text name=message value="" size="30" >

</form>

<input type=submit value=¡TAG! onClick="tag.submit()">


</body>
</html>

YES!! you have nailed it!. Thank you.:) :)

DrDaMour
04-16-2003, 01:10 PM
i messed with that to nedal, but it seems that the enter key defaults to a input type=button included within the form if there's not submit button. Pretty dumb eh, so again, you have to put the button OUTSIDE the form, then it'll work like a charm.

Nedals
04-16-2003, 01:11 PM
daveWho
That <input... > tag outside the <form> tags is WRONG and, althought it may sometimes work in IE, it will NEVER work in NS. You should not do that.

See my post above for the solution.

Nedals
04-16-2003, 01:18 PM
it seems that the enter key defaults to a input type=buttonI think you are saying that, if the submit button is ommitted, the enter key will activate the 'type=button'.

It doesn't on my IE6 browser!

DrDaMour
04-16-2003, 01:20 PM
nedals, if you look at my earliest posts, that's what i told him to do in teh beginning, but it doesn't work the way we'd expect it to. Like i said enter will default the the input button within teh form. Why wouldn't it work in netscape, is there no support for

form.submit() in ns?

DrDaMour
04-16-2003, 01:25 PM
nevermind nedals is right, you should not be using the type=submit. I got confused the right code is as follows



<html>
<body>

<form id="tag" action=tag.cgi method=get target=yes topmargin=0>


<input type=text name=name value="" size="10" >

<input type=text name=email value="" size="10" >
<input type=text name=message value="" size="30" >
<input type=button value=¡TAG! onClick="tag.submit()">

</form>



</body>
</html>

i could have sworn that's what i said to do in the beginning too.....

Nedals
04-16-2003, 01:33 PM
Why wouldn't it work in netscape, is there no support for form.submit() in ns?
Netscape will not display form elements unless they are contained within form tags. IE is flawed in that it will.

i could have sworn that's what i said to do in the beginning too.....
You did :) Hence my quote.. Change to this (alluded to above) and your problem is solved

daveWho
04-16-2003, 01:49 PM
Originally posted by nedals
daveWho
That <input... > tag outside the <form> tags is WRONG and, althought it may sometimes work in IE, it will NEVER work in NS. You should not do that.

See my post above for the solution.

But it DOES work for me (NS7).

Nedals
04-16-2003, 02:18 PM
But it DOES work for me (NS7).
Hmmmm :)
So NS has decided to include the same flaw as IE. I checked out NS7 and it does indeed show the button, but it doesn't do anything on mine. Did it actually submit in your NS7?
NS4 does not display the button.

I guess it's really a mute issue. The bottom line is that form elements should be contained within form tags (though others on this forum may argue the point)

:D :D

DrDaMour
04-16-2003, 02:23 PM
i agree with you, and that is what i said to do initially. but i got confused when he continually told me that it wasn't working, but i checked it and the last code i posted does work properly

daveWho
04-16-2003, 02:28 PM
Originally posted by DrDaMour
i agree with you, and that is what i said to do initially. but i got confused when he continually told me that it wasn't working, but i checked it and the last code i posted does work properly

Sorry guys, but I stand by what I reported. It doesn't work when button is inside <form..></form..>
But I also agree that it SHOULDN'T work when button is outside. It's always dubious coding to take deliberate advantage of a flaw.

DrDaMour
04-16-2003, 02:29 PM
did you change type=submit to type=button that was the mistake i kept making......

again this works perfect in IE:

<html>
<body>

<form id="tag" action=tag.cgi method=get target=yes topmargin=0>


<input type=text name=name value="" size="10" >

<input type=text name=email value="" size="10" >
<input type=text name=message value="" size="30" >
<input type=button value=¡TAG! onClick="tag.submit()">

</form>



</body>
</html>

daveWho
04-16-2003, 02:53 PM
Originally posted by DrDaMour
did you change type=submit to type=button that was the mistake i kept making......

again this works perfect in IE:

<html>
<body>

<form id="tag" action=tag.cgi method=get target=yes topmargin=0>


<input type=text name=name value="" size="10" >

<input type=text name=email value="" size="10" >
<input type=text name=message value="" size="30" >
<input type=button value=¡TAG! onClick="tag.submit()">

</form>



</body>
</html>

Hmmm. I wonder if method=get is making the difference. I'm using "post". My code is at www.itworldcanada.com/temp/surveyx.cfm. And it doesn't work.

DrDaMour
04-16-2003, 03:00 PM
no it's not post, but i did figure it out.

See isn't that nice of browsers to figure if there is only 1 form element, that an enter autosubmits. just add a seoncd text box, and you'll see taht hitting enter will just make a little beep noise.

That's pretty funny error, but it's YOUR FAult for only having one input :) j/k

daveWho
04-17-2003, 07:34 AM
You're right. Having multiple input fields does make a difference. How about we start lobbying W3C to provide "Works with W3C spec" stickers to compliant browsers:D

Thanks to everyone on this thread for their time and effort.