Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple form "action" items, is it possible? 1

Status
Not open for further replies.

peterv12

Technical User
Dec 31, 2008
108
0
0
US
I'm new to using HTML forms. I've got one working using:

Code:
<form name="form1" method="post" action="delete_in_form.php">

This runs the php code when a submit button is hit. I want to use several buttons, with each one executing a different php file. Is there a simple way to do that? I've done a search, but I've only found forms with a single "action" parameter. I appreciate any & all help.
 
Hi

You can not do that.

For an alternative JavaScript solution see thread216-1553880.

However I do not recommend it. Better keep the [tt]form[/tt] submitting to the same PHP script, then in that script do different actions based on which button submitted the [tt]form[/tt].

Feherke.
 
Thanks feherke, I had a feeling that doing it the way you suggest was correct. I just wanted to make sure it was the best way.
 
Just to make sure my brain is tracking Feherke, it would be:

original_form:

<form name="form1" method="post" action="processing_page.asp">
stuff in form
<input type="submit" name="Buy" value="Buy">
<input type="submit" name="Sell" value="Sell">
<input type="submit" name="Hold" value="Hold">
</form>

processing_page:
if request.form("submit") = "Buy" then
do one set of actions
end if

if request.form("submit") = "Sell" then
do a different set of actions
end if

if request.form("submit") = "Hold" then
do something entirely different
end if


??
 
Hi

I have no idea about ASP, but I would say no. I think the test should be [tt]request.form("[red]Buy[/red]") = "Buy"[/tt], because your [tt]input[/tt]'s [tt]name[/tt] is not "submit", but "Buy", and so on.

In PHP it would look like this :
PHP:
[b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]array_key_exists[/color][teal]([/teal][green][i]'Buy'[/i][/green][teal],[/teal][navy]$_POST[/navy][teal]))[/teal] [teal]{[/teal]
  [gray]// do one set of actions[/gray]
[teal]}[/teal] [b]elseif[/b] [teal]([/teal][COLOR=darkgoldenrod]array_key_exists[/color][teal]([/teal][green][i]'Sell'[/i][/green][teal],[/teal][navy]$_POST[/navy][teal]))[/teal] [teal]{[/teal]
  [gray]// do a different set of actions[/gray]
[teal]}[/teal] [b]elseif[/b] [teal]([/teal][COLOR=darkgoldenrod]array_key_exists[/color][teal]([/teal][green][i]'Hold'[/i][/green][teal],[/teal][navy]$_POST[/navy][teal]))[/teal] [teal]{[/teal]
  [gray]// do something entirely different[/gray]
[teal]}[/teal] [b]else[/b] [teal]{[/teal]
  [gray]// default action[/gray]
  [gray]/*[/gray]
[gray]    Note that the browsers behavior varies on what they do when[/gray]
[gray]    not a submit or image input submits the form.[/gray]
[gray]    ( For example Enter was pressed in a form element which not[/gray]
[gray]    handles Enter itself, or the submit() method was called. )[/gray]
[gray]    In such case the browser may send the first submit button's[/gray]
[gray]    name & value or send nothing related to submit buttons.[/gray]
[gray]  */[/gray]
[teal]}[/teal]
An alternative is to give the same [tt]name[/tt] to all [tt]submit[/tt] [tt]input[/tt]s :
HTML:
[b]<form[/b] [maroon]name[/maroon][teal]=[/teal][green][i]"form1"[/i][/green] [maroon]method[/maroon][teal]=[/teal][green][i]"post"[/i][/green] [maroon]action[/maroon][teal]=[/teal][green][i]"processing_page.asp"[/i][/green][b]>[/b]
stuff in form
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"samename"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Buy"[/i][/green][b]>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"samename"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Sell"[/i][/green][b]>[/b]
[b]<input[/b] [maroon]type[/maroon][teal]=[/teal][green][i]"submit"[/i][/green] [maroon]name[/maroon][teal]=[/teal][green][i]"samename"[/i][/green] [maroon]value[/maroon][teal]=[/teal][green][i]"Hold"[/i][/green][b]>[/b]
[b]</form>[/b]
PHP:
[b]switch[/b] [teal]([/teal][navy]$_POST[/navy][teal][[/teal][green][i]'samename'[/i][/green][teal]])[/teal] [teal]{[/teal]
  [b]case[/b] [green][i]'Buy'[/i][/green][teal]:[/teal]
    [gray]// do one set of actions[/gray]
  [b]break[/b][teal];[/teal]
  [b]case[/b] [green][i]'Sell'[/i][/green][teal]:[/teal]
    [gray]// do a different set of actions[/gray]
  [b]break[/b][teal];[/teal]
  [b]case[/b] [green][i]'Hold'[/i][/green][teal]:[/teal]
    [gray]// do something entirely different[/gray]
  [b]break[/b][teal];[/teal]
  [b]default[/b][teal]:[/teal]
    [gray]// default action[/gray]
[teal]}[/teal]
I prefer this later one, however for multilingual sites the first way is simpler.


Feherke.
 
An alternative is to give the same name to all submit inputs"

I haven't needed to do that, but I like the simplicity - I'm adding this one to my toolbox.[thumbsup2]
 
Thanks to you all! These tips work great. Is there a way to mark this question as solved? (I'm unsure if that's a requirement in this message board.)
 
Hi

peterv12 said:
Is there a way to mark this question as solved?
No, there is no such thing on Tek-Tips. The threads' status can only be open or closed. Closing is done automatically after a predefined amount of time without activity.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top