calling two Actions in a form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gen23
Forum Newbie
Posts: 7
Joined: Fri Apr 08, 2005 10:13 am

calling two Actions in a form

Post by gen23 »

Hi,

I am creating a php program with an HTML integration. Within my HTML code, I called two buttons... my question is, how can I call two different actions or page using a single form? Is this correct?

Code: Select all

<form method = &quote;POST&quote; action = &quote;/module/Default/action/Add&quote; , action = &quote;/module/Default/action/Search&quote;> 

<input type=&quote;submit&quote; name=&quote;submit&quote; value=&quote;submit&quote;> 
<input type=&quote;submit&quote; name=&quote;search&quote; value=&quote;search&quote;> 
<input type= &quote;text&quote; name=&quote;search&quote;>
Thank you.... :oops:
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

you can have only one "action" per form.

but as you already noticed, you can have multiple submit buttons, with various names/values...

so, for example:

Code: Select all

<form type=&quote;post&quote; action=&quote;foo.php&quote;>
<input type=&quote;submit&quote; name=&quote;action&quote; value=&quote;add&quote;/>
<input type=&quote;submit&quote; name=&quote;action&quote; value=&quote;search&quote;/>
</form>
if the user than clicks on the add button, you will have $_POST['action'] == 'add'. And if he clicks on the search button, $_POST['action'] will equal 'search'
gen23
Forum Newbie
Posts: 7
Joined: Fri Apr 08, 2005 10:13 am

Post by gen23 »

Does that mean that my form would look like this--> <form method="POST"> Since my two buttons will lead to a different page and placing a certain page on the action might become confusing?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Untested:

Code: Select all

<script Language=&quote;Javascript&quote;>

function goToPage1(){
  
 doc = eval(&quote;document.form1&quote;);
 doc.action = &quote;/module/Default/action/Add&quote;;
 doc.submit();
 
}
function goToPage2(){
  
 doc = eval(&quote;document.form1&quote;);
 doc.action = &quote;/module/Default/action/Search&quote;;
 doc.submit();
 
}
</script>
<form name=&quote;form1&quote; method = &quote;POST&quote;> 
 
<input type=&quote;submit&quote; name=&quote;submit&quote; value=&quote;submit&quote; onclick=&quote;goToPage1(); return false;&quote;> 
<input type=&quote;submit&quote; name=&quote;search&quote; value=&quote;search&quote; onclick=&quote;goToPage2(); return false;&quote;> 
<input type= &quote;text&quote; name=&quote;search&quote;>
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

i was need this to eheehe...

but if you don't want to use javascript and you want to preform two actions with one button form, use some php function to pass information to another page.. for example...


<form method=post action=1111.php>

-----------------------------

second action

Code: Select all

function 2_action() {

header (" location: page?value=xx ");

}
Post Reply