By jorje29 on
I have a form and a submit button with a function "team_add_submit" but I can't pass a variable(argument) with this function. Does anybody know how to do it ?
<?php
$form['set']['add'] = array(
'#type' => 'submit',
'#value' => t('Add Team'),
'#submit' => array('team_add_submit'),
);
function team_add_submit($form, &$form_state) {
//I need my variable(argument) here
}
?>
Comments
Hidden form field
Why not add your variable as an hidden form field in your form? Should be available then without the need for another variable passed through the called function... or did I miss something?
Stenjo, thanx for the
Stenjo, thanx for the reply
The thing is I don't have only one submit button in my form. Your solution would work with only one submit button. Now, that I have multiple submit buttons I need to pass a variable which shows me what button exactly was clicked. For example :
I see now in Form API
I see now in Form API reference -> http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
#submit
Used by: button, form, submit
Description: Indicates whether or not button should submit the form. When set for the form itself, indicates the submit handler for the form.
Values: TRUE or FALSE. For form, an array in the form array('submit_function_1' => array('arg1a', 'arg1b',..), 'submit_function_2' => array('arg2a', 'arg2b'..)).
If I understand well, 'arg1a' and 'arg1b' are arguments for 'submit_function_1' function. How can I get the values of 'arg1a' and 'arg1b' inside 'submit_function_1' function ?
In your form definition, pass
In your form definition, pass them as values:
You can access them in $form_state['values'] in the submit function.
Contact me to contract me for D7 -> D10/11 migrations.
Jay, I have the code below.
Jay,
I have the code below. If I use your solution, how can I know which submit button was clicked ? I need to pass $var with the function, not just having a $form_state['arg1a'] available.
?>
<?php$form['submit1'] =
And in the submit function:
Contact me to contract me for D7 -> D10/11 migrations.
thanx Jay Maybe it's the
thanx Jay
Maybe it's the solution I'm seeking but I'm not sure how to implement it into my code. I will try, but do you think it can be done ? I present my code below :
I'll be honest, I'm not sure
I'll be honest, I'm not sure what you are trying to do, and I don't want to read through all that code. But you asked how to pass a variable, and you asked how to tell which button had been pushed. I've told you how to do both, so you should be able to figure it out on your own. Or if you have a more specific question I can try to help. I just don't like going through piles of code trying to guess what someone is trying to do. Sorry!
Contact me to contract me for D7 -> D10/11 migrations.
You are right, I fully
You are right, I fully understand you. I apologize...
In your previous post, can you please explain me what is the value of
$form_state['values']['op']
$form_state['values']['submit1']
$form_state['values']['submit2']
?
Both $form['submit1'] and $form['submit2'] have #value t('Submit'). How we determine which submit button was clicked ?
Ahh, you're right actually.
Ahh, you're right actually. You will have to give different values to the two submit buttons.
$form_state['values']['op'] holds the value of the button that is pushed. So if it's equal to whatever submit1 was, then it means submit1 was clicked. If it's equal to submit2, then it means submit2 was pushed. Of course this doesn't work when they both have the same value! I was typing without thinking. But if you give them separate values, you can determine which was clicked.
Contact me to contract me for D7 -> D10/11 migrations.
Thank you very-very much Jay
Thank you very-very much Jay for your help
submit / image_button and $form_state['values']['op']
One more question :
I see that $form_state['values']['op'] value is whatever we set in #value property inside submit button.
If we change #type and we make it "image_button" and we keep #value the same, $form_state['values']['op'] has no value.
Is there a way to use #type => 'image_button' with #value => 'value' and when we click on the image to receive $form_state['values']['op'] = 'value' ??
Thanx
Gotta say, I've never tried.
Gotta say, I've never tried. Let me know if/when you figure out the answer!
Contact me to contract me for D7 -> D10/11 migrations.
If I find out something, I
If I find out something, I will let you know, promise...
Solution
Hello Jay
I found out that instead of $form_state['values']['op'] I can use $form_state['clicked_button']['#post']['field_name']. So, instead of a submit button and #type => 'submit' I can use a fancy image and #type => 'image_button'.
My code before :
My code now :