/    Sign up×
Community /Pin to ProfileBookmark

Javascript submitting wrong form

Hi,
I not sure if I identified the problem correctly. I’m using Javascript to submit forms in a HTML file. In this particular case I have three of them all with different target urls.
Yet, when I run the function submitting one form another form is submitted. When I have an alert just before the submit command it submits the correct one…
Here’s the code (or at least the relevant parts of it) so you’ll better understand.
First the html part:

[CODE]
<form method=”post” action=”refer_to_repair_activity.php” name=”existing_repair_activity_form” id=”existing_repair_activity_form”>
<input type=”hidden” id=”existing_repair_activity_id” name=”id” value=””>
<input type=”hidden” id=”existing_repair_activity_cid” name=”cid” value=””>
</form>

<form method=”post” action=”general_car_details.php” name=”general_car_details_form” id=”general_car_details_form”>
<input type=”hidden” id=”general_car_details_id” name=”id” value=””>
</form>

<form method=”post” action=”refer_to_repair_activity.php” name=”add_new_repair_activity_form” id=”add_new_repair_activity_form”>
<input type=”hidden” id=”add_new_repair_activity_id” name=”id” value=””>
</form>
[/CODE]

And here’s the Javascript function:

[CODE]
function submitExistingRepairActivity(rpID, carID) {
document.getElementById(“existing_repair_activity_id”).value = rpID;
document.getElementById(“existing_repair_activity_cid”).value = carID;
// alert(‘some annoying alert’);
document.forms[0].submit();
}
[/CODE]

The function will be called when a link is clicked which will then provide the required parameters.

The form submitted is always general_car_details_form.

I would be happy for some ideas as to why this happens, especially because an alert just before submitting the form helps, setting breakpoints in the javascript debug mode helps as well, even though this is not really an option ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@thisaintmeauthorJun 12.2012 — OK, it might have been something different, I think the link which I wanted to use to call the submit function was not working as intended.

I have the entire table row with an onclick-event calling one function and a link in that row calling another one.

I removed the onclick-Event from the link-cell. Now it works. Hope that was understandable ?
Copy linkTweet thisAlerts:
@PadonakJun 13.2012 — voo-doo magic:eek:
×

Success!

Help @thisaintme 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 6.1,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...