/    Sign up×
Community /Pin to ProfileBookmark

JavaScript and submitting forms

Hi there guys. I’ve been reading the posts on this forum for a while, and realised that you may be able to help me with a problem I’ve been having!

My problem is this: I want to, using JavaScript, when one form is submitted (using the enter key), I want to submit a different form. My code looks something like this:

[code=html]<form action=”#” id=”visform”>
<input type=”text” name=”username” />
<input type=”password” name=”password” />
</form>

<form action=”login.php” id=”hidform”>
<input type=”hidden” name=”hiduname” />
<input type=”hidden” name=”hidpass” />
<input type=”submit” value=”Submit” />
</form>[/code]

Using JavaScript, when the second form is submitted, I copy over the values from the first form to the second form, doing some hashing on the way. This method works fine, apart from the fact that I cannot submit on an enter key press if the cursor is currently inside one of the text fields. I’ve tried the following onsubmit event handler of the visform:

[CODE]onsubmit=”document.getElementById(‘hidform’).submit();”[/CODE]

but since the first form doesn’t have a submit button of its own, this does nothing, and including a hidden submit button only submits the first form on enter, not the second one.

Any / all help would be appreciated! I realise that all my sites should run fine with JavaScript disabled, but this is a private one being used by only a few people, so the necessity to have JavaScript on isn’t a big one. Thanks in advance for the help!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisDec 22.2009 — A form without a submit button will submit with the "enter" key if the cursor is in one of the text fields.

You should add ";return false" to your onsubmit event. Otherwise the other form will still submit and wipe out your work.
Copy linkTweet thisAlerts:
@KorDec 22.2009 — Bizarre HTML structure? Why do you need a "hidden" form?
×

Success!

Help @sparksy 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.18,
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,
)...