473,480 Members | 4,939 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

2 submits buttons for a form.

TheServant
1,168 Recognized Expert Top Contributor
Hey guys,
Well what I want is to have one form, but two submit buttons. Depending on which submit button is pressed, different code will run.

Here is a short version of my form:
[HTML]<form action="myform.php" method="POST">
<input name="var1" type="text" value="0" size="7" maxlength="6" />
<input name="var2" type="text" value="0" size="7" maxlength="6" />
<input name="var3" type="text" value="0" size="7" maxlength="6" />
<input name="var4" type="text" value="0" size="7" maxlength="6" />
<input name="recruit" type="submit" value="Recruit" />
<input name="disband" type="submit" value="Disband" />
</form>
[/HTML]

The php check:
[PHP]if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['var1']) && isset($_POST['var2']) && isset($_POST['var3']) && isset($_POST['var4']) )
{ //do stuff
}[/PHP]

So can I add something like && $_submit_name == 'recruit' ???

Also, I know this is a php forum, but do you also know the javascript offhand:
[HTML]if ( document.myform.var1.value == "" && document.myform.var2.value == "" && document.myform.var3.value == "" && document.myform.var4.value == "" )[/HTML]
Apr 12 '08 #1
14 1705
Markus
6,050 Recognized Expert Expert
Hey guys,
Well what I want is to have one form, but two submit buttons. Depending on which submit button is pressed, different code will run.

Here is a short version of my form:
[HTML]<form action="myform.php" method="POST">
<input name="var1" type="text" value="0" size="7" maxlength="6" />
<input name="var2" type="text" value="0" size="7" maxlength="6" />
<input name="var3" type="text" value="0" size="7" maxlength="6" />
<input name="var4" type="text" value="0" size="7" maxlength="6" />
<input name="recruit" type="submit" value="Recruit" />
<input name="disband" type="submit" value="Disband" />
</form>
[/HTML]

The php check:
[PHP]if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['var1']) && isset($_POST['var2']) && isset($_POST['var3']) && isset($_POST['var4']) )
{ //do stuff
}[/PHP]

So can I add something like && $_submit_name == 'recruit' ???

Also, I know this is a php forum, but do you also know the javascript offhand:
[HTML]if ( document.myform.var1.value == "" && document.myform.var2.value == "" && document.myform.var3.value == "" && document.myform.var4.value == "" )[/HTML]
I don't quite understand your coding.
You check if your text inputs are set, but theyre always set.
Also, i dont understand the usefullness of 'request_method'?

Anyway, how i would do this is:
[php]
if(isset($_POST['recruit']))
{
# do recruit stuff
}
elseif(isset($_POST['disband']))
{
# do disband stuff
}
else
{
# nothing submitted
}
[/php]
Apr 12 '08 #2
Amzul
130 New Member
use javascript submit()
each button onclick to send different data to the server
Apr 12 '08 #3
ronverdonk
4,258 Recognized Expert Specialist
use javascript submit()
each button onclick to send different data to the server
I cannot see how javascript could help here and work better than php.

Just take the example markusn00b showed and you'll be fine. JS just clouds this dicusssion.

Ronald
Apr 12 '08 #4
TheServant
1,168 Recognized Expert Top Contributor
I don't quite understand your coding.
You check if your text inputs are set, but theyre always set.
Also, i dont understand the usefullness of 'request_method'?
Cheers markus, yeah you're right about the request_method, it was an old form I was reformatting, forgot to take it out. I will try your code when I get home.

I am concerned about your question about my text inputs...
I have a few chunks of code to { do stuff } so when the form submits information to that file, it will send only the variables contained in that form. So by checking isset() I thought what I was doing was making sure it was the correct information for that function.

ie.
<body>
form1 {var1 var2 var3}
form2 {var4 var5 var6}
</body>

do_stuff.php {form1_function form2_function}


If I submit form1, form1_function checks if var1, 2 and 3 are set (which they are) before it runs, but when the reader gets down to form2_function, var4, 5 and 6 are not set (because I only submitted form1) so it will not run.
Is this not how it works?
Apr 13 '08 #5
Markus
6,050 Recognized Expert Expert
See the below post

Regards.
Apr 14 '08 #6
Markus
6,050 Recognized Expert Expert
Oh, i see.

Well, you can bypass all that with using the way i suggested in my prev. post.

Regards :)

ps: i thought you were checking that the inputs had a value and all of them had to have a value before the IF statement would run - this is flawed because text inputs' are always passed in a form, empty or not.
Apr 14 '08 #7
dlite922
1,584 Recognized Expert Top Contributor
Hey guys,
Well what I want is to have one form, but two submit buttons. Depending on which submit button is pressed, different code will run.

Here is a short version of my form:
[HTML]<form action="myform.php" method="POST">
<input name="var1" type="text" value="0" size="7" maxlength="6" />
<input name="var2" type="text" value="0" size="7" maxlength="6" />
<input name="var3" type="text" value="0" size="7" maxlength="6" />
<input name="var4" type="text" value="0" size="7" maxlength="6" />
<input name="recruit" type="submit" value="Recruit" />
<input name="disband" type="submit" value="Disband" />
</form>
[/HTML]

The php check:
[PHP]if ( $_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['var1']) && isset($_POST['var2']) && isset($_POST['var3']) && isset($_POST['var4']) )
{ //do stuff
}[/PHP]

So can I add something like && $_submit_name == 'recruit' ???

Also, I know this is a php forum, but do you also know the javascript offhand:
[HTML]if ( document.myform.var1.value == "" && document.myform.var2.value == "" && document.myform.var3.value == "" && document.myform.var4.value == "" )[/HTML]
Another way I do it, is the NAME of the buttons are the same but the values are not, so i just get the value of the button and run appropriate action.

[PHP]

<input type="submit" name="pageAction" value="Action One" />
<input type="submit" name="pageAction" value="Action Two" />

[/PHP]

then on php side

[PHP]

if($_POST['pageAction'] == "Action One")
{
// do something
}
else if ($_POST['pageAction'] == "Action Two")
{
// do something else
}

[/PHP]

Anybody know which way is better? I'll change the way markus is doin it if that's better.
Apr 14 '08 #8
Markus
6,050 Recognized Expert Expert
Another way I do it, is the NAME of the buttons are the same but the values are not, so i just get the value of the button and run appropriate action.

[PHP]

<input type="submit" name="pageAction" value="Action One" />
<input type="submit" name="pageAction" value="Action Two" />

[/PHP]

then on php side

[PHP]

if($_POST['pageAction'] == "Action One")
{
// do something
}
else if ($_POST['pageAction'] == "Action Two")
{
// do something else
}

[/PHP]

Anybody know which way is better? I'll change the way markus is doin it if that's better.
There won't be any significant difference; there's just more writing in your way :P
Apr 14 '08 #9
TheServant
1,168 Recognized Expert Top Contributor
Didn't get a chance to try it, but both are logical. I will try both, but thanks for your help guys. Also interesting comment about text inputs markus, I didn't know that.
Apr 14 '08 #10
Markus
6,050 Recognized Expert Expert
Didn't get a chance to try it, but both are logical. I will try both, but thanks for your help guys. Also interesting comment about text inputs markus, I didn't know that.
Ah, no problem ma' man!
Apr 14 '08 #11
TheServant
1,168 Recognized Expert Top Contributor
I don't quite understand your coding.
You check if your text inputs are set, but theyre always set.
Also, i dont understand the usefullness of 'request_method'?

Anyway, how i would do this is:
[php]
if(isset($_POST['recruit']))
{
# do recruit stuff
}
elseif(isset($_POST['disband']))
{
# do disband stuff
}
else
{
# nothing submitted
}
[/php]
I know this is a php forum but do you (markus or anyone else) know how to do the equivalent in javascript?
Apr 16 '08 #12
Markus
6,050 Recognized Expert Expert
I know this is a php forum but do you (markus or anyone else) know how to do the equivalent in javascript?
I can give it a shot:
Expand|Select|Wrap|Line Numbers
  1. function doForm(buttonPressed)
  2. {
  3.     if(buttonPressed == "Recruit")
  4.     {
  5.         // do something
  6.         alert(buttonPressed);
  7.     }
  8.     else // 'disband' was pressed
  9.     {
  10.         # do something
  11.         alert(buttonPressed);
  12.     }
  13. }
  14.  
Expand|Select|Wrap|Line Numbers
  1. <form name="someForm" method="post" action="page.php">
  2.  <input type="text" name=" ...
  3.  
  4.  
  5.  
  6.  
  7.  <input type="submit" name="recruit" onclick="doForm(this.value)" value="recruit" />
  8.  
  9.    <!-- you get the idea.. //-->
  10.  
  11.  
I'm not expert with javascript though, so maybe you should ask over there?
Apr 16 '08 #13
TheServant
1,168 Recognized Expert Top Contributor
I can give it a shot:
Expand|Select|Wrap|Line Numbers
  1. function doForm(buttonPressed)
  2. {
  3.     if(buttonPressed == "Recruit")
  4.     {
  5.         // do something
  6.         alert(buttonPressed);
  7.     }
  8.     else // 'disband' was pressed
  9.     {
  10.         # do something
  11.         alert(buttonPressed);
  12.     }
  13. }
  14.  
Expand|Select|Wrap|Line Numbers
  1. <form name="someForm" method="post" action="page.php">
  2.  <input type="text" name=" ...
  3.  
  4.  
  5.  
  6.  
  7.  <input type="submit" name="recruit" onclick="doForm(this.value)" value="recruit" />
  8.  
  9.    <!-- you get the idea.. //-->
  10.  
  11.  
I'm not expert with javascript though, so maybe you should ask over there?
Sounds good, I will give it a go, but make a post... "OVER THERE"... on the other side (excuse the pun).
Apr 16 '08 #14
rhys
25 New Member
Another way I do it, is the NAME of the buttons are the same but the values are not, so i just get the value of the button and run appropriate action.

[PHP]

<input type="submit" name="pageAction" value="Action One" />
<input type="submit" name="pageAction" value="Action Two" />

[/PHP]

then on php side

[PHP]

if($_POST['pageAction'] == "Action One")
{
// do something
}
else if ($_POST['pageAction'] == "Action Two")
{
// do something else
}

[/PHP]

Anybody know which way is better? I'll change the way markus is doin it if that's better.
You Da Man. Works for me!!!!
Jun 6 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1944
by: Robert V | last post by:
Hi all, I could use some help programming on of my Perl script to handle different submit buttons within the same form. Here is what I have so far. A user goes to a Web form and inputs some data...
2
1227
by: James Baker | last post by:
Once again, trying to re-learn ASP with all of my might. Probably a simple problem, but here goes: I have a form that current has a fair amount of data/input tags and a button that submits it...
8
3056
by: news | last post by:
I seriously doubt this is possible...but you never know, so here goes. Due to bad pre-planning I have a Web page that is VERY table heavy with a very complicated and delicate setup. Any changes...
1
2058
by: Mad Scientist Jr | last post by:
I'm stuck trying to work with a HTML <SELECT> control and javascript (similar to DualList but that control doesn't offer enough options to totally control the text on the buttons and control, also...
3
1720
by: Peet | last post by:
Hi there, Do somebody have some ideas about this behaviour? I have a struts application. It has a jsp which has a form and two buttons, one of them is html:submit, and the other is...
4
1435
by: Hul Tytus | last post by:
comp.infosystems.www.authoring.html multiple submits When multiple submit lines are placed in an HTML form, the string returned to the server shows the name=value field for each one with no...
2
1109
harshmaul
by: harshmaul | last post by:
Hi all, I have a problem. I have a search box on the top of all of my pages, and on various other pages i have forms that do other fancy stuff. However this has given rise to the bug. If i...
14
3780
Plater
by: Plater | last post by:
I'm going to stab myself in the face. I have a page with a single form. Regular old html. There are a few checkboxes and textboxes and and two submit buttons (I hope that's not the issue...) The...
6
1979
by: 0utlawza | last post by:
Hi Guys It seems i posted this in the incorrect topic, so i am reposting here. Please excuse the Newbie question. I am not really a programmer so excuse me if i dont clarify my point...
0
6905
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6908
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
2994
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.