/    Sign up×
Community /Pin to ProfileBookmark

One form.. two submit buttons

I have a form which has two submit buttons. I did so because I want every button to do something different than the other, that means when I click one I should be taken to a page, and when I click the other I should be taken to another page. It doesn’t seem to be possible, because the page is already defined in the (action) attribute, but maybe there is another way. Is there one?

And if there is not, is there an ability to distinguish between clicking any of the two buttons, like giving a field a certain value when one is clicked and another value when the other is clicked?

to post a comment
HTML

6 Comments(s)

Copy linkTweet thisAlerts:
@ray326Jul 28.2006 — The value of the action doesn't define a page, it defines a form handler. The form handler can route the request based on the value of the submit button.
Copy linkTweet thisAlerts:
@ashraf_gawdatauthorJul 28.2006 — I see, so how can I make the form handler depends on the value of the submit button?
Copy linkTweet thisAlerts:
@pacerierAug 05.2006 — if you mean that you have two input buttons and each one submits to a different page, you can do it easily by maniputating the action attribute of the form.

assuming your form is:
[code=html]<form name="form1" action="index.php" method="post">[/code]

you can submit it to another page through this:
[code=html]<input type="button" onclick="document.form1.action='pagex.php'; document.form1.submit();">[/code]
Copy linkTweet thisAlerts:
@ashraf_gawdatauthorAug 05.2006 — That seems to be a suitable solution, thanks.

I've already found another solution but with two conditions: when submitting the form from images not buttons, and submitting to one page not different pages, but with action depends on the submit image. So if one image name is "image1" and the form method was "post" I can can check in the target page weather $_POST[image1_x] or $_POST[image1_y] exists, then do what I want to do, and of course that can be done to any number of images.

But in case of submitting is done by buttons not images or submitting to different pages not the same page, pacerier's solution must take place.
Copy linkTweet thisAlerts:
@ray326Aug 05.2006 — You're misusing images submission if you are not using the x,y coordinates of the click.
Copy linkTweet thisAlerts:
@felgallAug 05.2006 — The value of the submit button that is selected gets passed with the form so you can always test which of the two values was passed to decide what to do. Of course you haven't said what you want to happen when the form is submitted without using either button (eg. directly from the keyboard).
×

Success!

Help @ashraf_gawdat 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.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

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