/    Sign up×
Community /Pin to ProfileBookmark

newbie question- form gets submitted twice

Hello I have written the following HTML page:

[code]
<html>
<body>
<form name=”search” method=”get” action=”b.php”>
<input type=”text” name=”query” id=”query” onkeydown=”if (window.event.keyCode == 13) { this.form.submit(); }” value=””></input>
</form>
</body>
</html>
[/code]

sometimes when I press ‘Enter’ in the textfield, the target page b.php gets called twice.
Does anyone know what is causing it?
sometimes, it gets called only once.

I would appreciate any help in explaining what I am doing wrong

to post a comment
HTML

5 Comments(s)

Copy linkTweet thisAlerts:
@AdamGundrySep 27.2004 — It could be the onkeydown event handler, which is unnecessary. The browser's default behaviour is that return submits the active form, and you don't need to add JavaScript to do that.

Adam
Copy linkTweet thisAlerts:
@jasongrauthorSep 27.2004 — The browser's default behaviour is that return submits the active form[/QUOTE]
What do you mean by return?

How can I cause the form to be submitted when the 'Enter' is pressed?
Copy linkTweet thisAlerts:
@AdamGundrySep 27.2004 — Return means the Enter key. You don't need to do anything for enter to submit a form, because it happens automatically.

Adam
Copy linkTweet thisAlerts:
@Stephen_PhilbinSep 27.2004 — Provided focus is not placed on a <textarea> at the time. But then having the enter button submit a form that has a <textarea> in it is a really dumb idea in the first place.
Copy linkTweet thisAlerts:
@fredmvSep 28.2004 — [i]Originally posted by jasongr [/i]

[B]How can I cause the form to be submitted when the 'Enter' is pressed? [/B][/QUOTE]
There is nothing you actually have to add. As Adam already mentioned, it's the default browser behavior to submit the form when the Enter key is pressed. You could, however, include a submit button:&lt;input type="submit" value="Submit"&gt;This is a good idea for the sake of usability. Some users may not know that Enter submits the form.
×

Success!

Help @jasongr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...