/    Sign up×
Community /Pin to ProfileBookmark

Dynamically Changing the Form tag’s "action" attribute

Is it possible to dynamically change the “[B]action[/B]” attribute in a <form> tag using two or more “submit” buttons on a form?

i.e. I have a javascript form that has two <input> buttons, one to create a file, and the second to download a file. Both buttons are to call an ASP page where processing of the information is done on the server; however if I set the “[B]action[/B]” attribute to the create process, how can I dynamically set it to the download process if this <input> button of <type=”submit”> is clicked instead? Any suggestions is appreciated.

lbyii

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJul 11.2006 — The better way to handle this is to have your ASP code detect which submit button was pressed and perform the appropriate associated action. No Javascript code would be required for that. But, if you want a JavaScript solution:

onclick="[COLOR=Blue]this.form.action[/COLOR]='new action'; return true;"

Don't change the part in blue. ?
Copy linkTweet thisAlerts:
@CharlesJul 11.2006 — Easily done, [font=monospace]this.form.action = 'some-script.pl'[/font], but do be extremely careful. You'll make the page unusable for quite a lot of people unless the form's original action attribute points to a JavaScript free fall-back.
Copy linkTweet thisAlerts:
@lbyiiauthorJul 11.2006 — phpnovice,

Thanks for your help; You recommended that the most efficient approach would be to have the ASP page detect which "submit" button was pressed/selected. If I were to move in this direction I am not sure of the code/syntax required for the detection. Any examples? Thanks in advance.

lbyii
Copy linkTweet thisAlerts:
@phpnoviceJul 11.2006 — It would have to be actual submit buttons in order to not use any JavaScript at all. For that, place these separate or together anywhere inside of your form:

<input type="submit" name="cmdCreate" value="Create">

<input type="submit" name="cmdDownload" value="Download">

The ASP for this would be:
&lt;%
If Request.Form("cmdCreate") = "Create" Then
'create code
End If
If Request.Form("cmdDownload") = "Download" Then
'download code
End If
%&gt;

You see, <input type="submit"> buttons won't transmit their value to the server side unless they are clicked. Note, though, that the HTML4 <button type="submit"> element is not so cooperative. ?

Otherwise, much the same effect can also be accomplished with radio buttons. Even non-submitting buttons could be used -- but without changing the form's [b]action[/b] property and using just a little JavaScript code. If you want to hear any more about these last two options (radio buttons vs. non-submitting buttons), just say so. ?
×

Success!

Help @lbyii 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.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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