By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,314 Members | 2,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,314 IT Pros & Developers. It's quick & easy.

Submitting forms with Javascript/Konqueror

P: n/a
Hi,

I have a rather long form that I don't want people submitting unless
they are absolutely sure they are ready.

I figured the easiest way to prevent an accidental form submission by
way of hitting of the enter key was to simply not have a submit button,
instead going for a regular anchor which calls a javascript confirmation
box and then submits the form.

To do this, I have one function, and an anchor that calls it:

function submitConfirm()
{
var submitConfirmed = confirm("Are you ready to submit this form?");
if( submitConfirmed == true )
{
document.surveyForm.submit();
}
....
}

<a href="#" onclick="submitConfirm()"><img src="submitBtn.png"
alt="Submit"></a>
All works fine in Mozilla and IE, but Konqueror doesn't behave as
expected. Instead of submitting the form, the anchor (in this case,
'http://my.site/BrokenSurvey.html#') is loaded instead.

I tried it using getElementsByName, but with the same result.

Am I doing something wrong, or is it time to file a bug report with the
KDE folks?

thanks,
Greg
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Greg T wrote:
I have a rather long form that I don't want people
submitting unless they are absolutely sure they are ready.

I figured the easiest way to prevent an accidental form
submission by way of hitting of the enter key was to simply
not have a submit button, instead going for a regular
anchor which calls a javascript confirmation box and then
submits the form.
You figured wrong. There is one obvious, simple and universally
supported (subject of JS availability) palace to intercept the
submission of any form and that is the - onsubmit - handler of the FORM
element.

With its use you can have the submit button that you should have (thus
avoiding any JS dependency in this aspect of the system, and
inconsistencies in the handling of your approach).

<form name="surveyForm" action=" ... " method="post"
onsubmit="return confirm('Are you ready to submit this form?');">
To do this, I have one function, and an anchor that calls it:

function submitConfirm()
{
var submitConfirmed = confirm("Are you ready to submit this form?");
if( submitConfirmed == true )
{
document.surveyForm.submit();
}
...
}
Would have been simpler as:-

function submitConfirm(){
if(confirm("Are you ready to submit this form?")){
document.surveyForm.submit();
}
}
<a href="#" onclick="submitConfirm()"><img
src="submitBtn.png" alt="Submit"></a>
When an - onclick - handler does not return boolean false it is not
cancelling the navigation specified in the HREF. It is normal for
browsers to assume that the last interaction with a page in terms of
form submitting or navigation represents the user's current desire. So
navigating to the specified HREF should be the expected result of this
operation because the navigation follows the - onclick - function's
execution.

You could change the onclick attribute to "submitConfirm();return
false;" and cancel the navigation, but you would be better off using the
FORM element's onsubmit handler.
All works fine in Mozilla and IE,
Consider yourself luck that it does because that is not the behaviour
that you code is strictly asking for.
but Konqueror doesn't behave as
expected.
Instead of submitting the form, the anchor (in this case,
'http://my.site/BrokenSurvey.html#') is loaded instead.
But it is doing what you are asking for in this case. i.e. it submits
the form (an asynchronous process that takes some time) and then it is
navigating to follow the HREF, cancelling the pending submission as it
is no longer interested in displaying the returned response.
I tried it using getElementsByName, but with the same result.
I can see no relevance in this.
Am I doing something wrong, or is it time to file a bug
report with the KDE folks?


There is no point in bothering the KDE folks with this, including a
submit button and using the - onsubmit - handler will produce
cross-browser results and consistent behaviour.

Richard.
Jul 23 '05 #2

P: n/a
Richard Cornford wrote:
Greg T wrote:
I have a rather long form that I don't want people
submitting unless they are absolutely sure they are ready.

I figured the easiest way to prevent an accidental form
submission by way of hitting of the enter key was to simply
not have a submit button, instead going for a regular
anchor which calls a javascript confirmation box and then
submits the form.

You figured wrong. There is one obvious, simple and universally
supported (subject of JS availability) palace to intercept the
submission of any form and that is the - onsubmit - handler of the FORM
element.

With its use you can have the submit button that you should have (thus
avoiding any JS dependency in this aspect of the system, and
inconsistencies in the handling of your approach).

<form name="surveyForm" action=" ... " method="post"
onsubmit="return confirm('Are you ready to submit this form?');">
<a href="#" onclick="submitConfirm()"><img
src="submitBtn.png" alt="Submit"></a>

When an - onclick - handler does not return boolean false it is not
cancelling the navigation specified in the HREF. It is normal for
browsers to assume that the last interaction with a page in terms of
form submitting or navigation represents the user's current desire. So
navigating to the specified HREF should be the expected result of this
operation because the navigation follows the - onclick - function's
execution.

You could change the onclick attribute to "submitConfirm();return
false;" and cancel the navigation, but you would be better off using the
FORM element's onsubmit handler.

....

Thanks for your help, Richard. I was hoping it wouldn't come to the
onsubmit handler. I was looking for a way that would fail if javascript
was disabled, rather than submit the form without prompting.

Looks like your way is good, if I chuck in some detection code.

thanks again,

Greg
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.