Question regarding onSubmit()

D

DrKen

I'm working on a task that involves adding code to a web page in
order to store the user id in a cookie when the "Submit" button is
clicked. This is a new thing for me and my JavaScript skills are
minimal. After looking at several tutorials and the book I have, I
still have a basic question. If the current page has this,
<form name="FORM" action="" method="POST">
and I want the page to run a script when the user clicks "Log in" (the
submit button), do I code
<form name="FORM" action="" method="POST" onSubmit='return
createCookie()'>

Or, do I code
<form name="FORM" onSubmit='return createCookie()'>

?

The examples I'm seeing all show a minimal form statement without a
method at all. I thought that if you did't specify a method you got
"get" as the default. Are method=post and onSubmit='return
someJSfunction()' mutually exclusive? Thanks.

Ken
 
G

Gregor Kofler

Am 2011-06-15 20:05, DrKen meinte:
I'm working on a task that involves adding code to a web page in
order to store the user id in a cookie when the "Submit" button is
clicked. This is a new thing for me and my JavaScript skills are
minimal. After looking at several tutorials and the book I have, I
still have a basic question. If the current page has this,
<form name="FORM" action="" method="POST">
and I want the page to run a script when the user clicks "Log in" (the
submit button), do I code
<form name="FORM" action="" method="POST" onSubmit='return
createCookie()'>

It's "onsubmit", without camel-case.
Or, do I code
<form name="FORM" onSubmit='return createCookie()'>

Which would result in the default method GET.
You choose whatever your prefered method of submitting your form data is.
The examples I'm seeing all show a minimal form statement without a
method at all. I thought that if you did't specify a method you got
"get" as the default. Are method=post and onSubmit='return
someJSfunction()' mutually exclusive? Thanks.

No. Why should they? The submit event fires *before* the form is
submitted (the listener function - or with inline listeners their return
value - can cancel the form submission).

Once your submit-listener has run, and the browser is in charge of
submitting the form, the method attribute will be of interest.

Gregor
 
T

Thomas 'PointedEars' Lahn

Gregor said:
Am 2011-06-15 20:05, DrKen meinte:

It's "onsubmit", without camel-case.

In *HTML* (but not in proprietary DOM scripts) that is really a matter of
taste (and desire for XHTML compatibility).


PointedEars
 
J

Jukka K. Korpela

I'm working on a task that involves adding code to a web page in
order to store the user id in a cookie when the "Submit" button is
clicked. [...]
<form name="FORM" action="" method="POST" onSubmit='return
createCookie()'>

Or, do I code
<form name="FORM" onSubmit='return createCookie()'>

As Gregor Kofler replied, that really depends on the desired method of
form submission, which in turn depends on several factors as is external
to JavaScript.

But there's another issue here. The submit event (handled by the
onsubmit handler) fires when the form is about to be submitted by the
user*), and this is not quite the same thing as clicking on a "Submit"
button. The form might be submitted in other ways as well, like pressing
Enter in a text input field. And clicking on a "Submit" button might be
handled by an event handler that prevents its normal operation, the
submission.

Usually the difference does not matter, but sometimes it does. Normally
what you want is code that is executed on submission, not on clicking
the submit button. But if you really want the latter, you would use the
onclick="..." attribute in the button element, as in
<input type="submit" onclick="return createCookie()">

*) But not in "scripted-submit", i.e. when the submit() method is used
in JavaScript.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top