471,618 Members | 1,315 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,618 software developers and data experts.

Attaching second value to a form submit button.

bugboy
160 100+
I only have a basic understanding of html. What i want to do is have a group of form submit buttons within the same form but i need each button to pass on two values unique to it's self (one is hidden). If i use a <input type="hidden"
Oct 28 '07 #1
5 9179
Lumpy
69
I only have a basic understanding of html. What i want to do is have a group of form submit buttons within the same form but i need each button to pass on two values unique to it's self (one is hidden). If i use a <input type="hidden"
Yes, you can have multiple submit buttons on a single form, however if you have a hidden field within the form, each submit button is going to submit that field. If each button needs to pass a certain variable, you can assign the value of the variable to the button instead of a hidden field, that way each button will submit a different value. I once did something like this just for fun, where I used submit buttons as a menu to navigate a site. The code I used had the buttons with different values of each button would determine what content to load. You will probably want to keep each value the same and give each button a different name. Then using php, you can check which button was clicked.

Form would be something like
Expand|Select|Wrap|Line Numbers
  1.  
  2.  <form method="post" action="index.php">
  3.  <input name="val1" type="submit" value="submit" />
  4.  <input name="val2" type="submit" value="submit" />
  5.  <input name="val3" type="submit" value="submit" />
  6.  <input name="val4" type="submit" value="submit" />
  7.           </form>
  8.  
  9.  
and then using php to determine the button that was clicked would be something like...
Expand|Select|Wrap|Line Numbers
  1. if($_POST['val1']) {
  2. ///code for val1
  3. }
  4.  
Good luck and hope this helps!
Oct 28 '07 #2
bugboy
160 100+
Thanks! Yes this is what i would like to do but each of the submit buttons needs to pass on two values. I suppose i could glue the two values into a string then explode them on the new page.

I don't think the below code is valid but it represents the idea i'm looking for...

[HTML]
<form method="post" action="index.php">
<input name="name" type="submit" value1="$val1" value2="$val2"/>
<input name="name" type="submit" value1="$val3" value2="$val4"/>
</form>[/HTML]

The only reason they need to be in the same form is for looks, this the only way i can figure out to get buttons to sit side by side without creating multiple <td>'s to hold each. I want them side by side and in the same <td> so that they wrap and build up multiple rows of buttons on the screen instead of one column. Maybe i can get around this by abandoning tables and using CSS?

more:
The buttons are created in a 'while' loop so the number will vary. I need to keep them with the same name so that i don't have redundant php code which has to look for the max number of possible buttons every time... hmmm unless i put that in a loop which would count up through buttons until it found the one that was submitted... it would just be so much easier if there was a way to submit two values for each button in the form....
Oct 28 '07 #3
Lumpy
69
The buttons within the same <td> should display in a row. The code I gave you displays them in a row in IE and Firefox. Using CSS on top of that you can make the buttons look and behave in many different ways. The next thing is that the button will say what is in the value field. If this is ok, then you could assign the different values and probably be ok with it, but both values would have to be under one 'value' tag. You can't have two value tags in the same button. If this doesn't work, then you might have to assign each button a different name. Maybe a combination of different 'name' and 'value' attributes will get you what you need, or like you mentioned, use the explode() function to get each value you need. I am not sure on the look and exactly what your doing, but with a combination of these ideas, it sounds like you can get it.

As far as your second loop idea is concerned, if you go that route I would look into a switch statement within php. This might be helpful in writing one loop that will handle the form correctly depending on which button is pushed.

Good Luck!
Oct 28 '07 #4
bugboy
160 100+
Thanks for your help lumpy! I think i'll use a string and explode it on the other end.. it seems to be the most flexible and has the least redundancy.

BugBoy
Oct 28 '07 #5
Lumpy
69
Thanks for your help lumpy! I think i'll use a string and explode it on the other end.. it seems to be the most flexible and has the least redundancy.

BugBoy
No Problem. Glad I was able to be of some help. :D

Post back if you have any other questions.
Oct 28 '07 #6

Post your reply

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

Similar topics

7 posts views Thread by r0adhog | last post: by
6 posts views Thread by Oleg Konovalov | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.