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

stenjo’s picture

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?

jorje29’s picture

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 :

<?php

for ($x = 0; $x <10; $x++) {

 $form['set']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add Team'),
    '#submit' => array('team_add_submit'),
    // $var = $x;  // this is the variable I need to pass with team_add_submit function
    );

}

function team_add_submit($form, &$form_state) {

//I need my variable($var) here

}

?>
jorje29’s picture

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 ?

<?php

function submit_function_1() {

//I need 'arg1a' and 'arg1b' here

}

?>
jaypan’s picture

In your form definition, pass them as values:

$form['arg1a'] = array
(
  '#type' => 'value',
  '#value' => $arg1a,
);
$form['arg1b'] = array
(
  '#type' = 'value',
  '#value' => $arg1b,
);

You can access them in $form_state['values'] in the submit function.

Contact me to contract me for D7 -> D10/11 migrations.

jorje29’s picture

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

for ($x = 0; $x <10; $x++) {

$form['set']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add Team'),
    '#submit' => array('team_add_submit'),
    // $var = $x;  // this is the variable I need to pass with team_add_submit function
    );

}

function team_add_submit($form, &$form_state) {

//I need my variable($var) here

}
?>

?>

jaypan’s picture

$form['submit1'] = array
(
  '#type' => 'submit',
  '#value' => t('Submit'),
);
$form['submit2'] = array
(
  '#type' => 'submit,
  '#value' => t('Submit'),
);

And in the submit function:

if($form_state['values']['op'] == $form_state['values']['submit1'])
{
  /// submit 1 was pressed
}
elseif($form_state['values']['op'] == $form_state['values']['submit2'])
{
 // submit 2 was pressed
}

Contact me to contract me for D7 -> D10/11 migrations.

jorje29’s picture

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 :

<?php

function mymodule_form(&$node, $form_state) {

$form['set'] = array(
    '#type' => 'fieldset',
    '#title' => t('Teams'),
    );

if (isset($form_state['team_count'])) {
    $team_count = $form_state['team_count'];
    }
    else {
    $team_count = 1;
    }

for ($x = 0; $x < $team_count; $x++) {

$team = isset($node->teamname[$x]['name']) ? $node->teamname[$x]['name'] : '';

$form['set']['teamname'][$x] = _team_add_form($team);

if ( $x == 0 ) { if (isset($form_state[$player_count1])) { $player_count = $form_state[$player_count1]; } else { $player_count = 1; } }
elseif ( $x == 1 ) { if (isset($form_state[$player_count2])) { $player_count = $form_state[$player_count2]; } else { $player_count = 1; } }
elseif ( $x == 2 ) { if (isset($form_state[$player_count3])) { $player_count = $form_state[$player_count3]; } else { $player_count = 1; } }

for ($z = 0; $z < $player_count; $z++) {

$player = isset($node->teamset['pset']['playername'][$z]['pname']) ? $node->teamset['pset']['playername'][$z]['pname'] : '';
   
$form['set']['teamname'][$x]['teamset']['pset']['playername'][$z] = _player_add_form($player);

$form['set']['teamname'][$x]['teamset']['pset']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add Player'),
    '#submit' => array('player_add_submit'),
    );

}

}

function team_add_submit($form, &$form_state) {

$form_state['team_count'] = count($form_state['values']['teamname'])+1;
$form_state['rebuild'] = true;

}

function player_add_submit($form, &$form_state) {

$form_state['rebuild'] = true;

// here is where I need the passing variable form submit button for knowing which button was clicked and increment by one the player fields in the appropriate team

}

function _team_add_form($team = '') {
    
    $form['teamset'] = array(
    '#type' => 'fieldset',
    '#title' => t('Fieldset'),
    );
    
    $form['teamset']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Team @n', array('@n' => ($x + 1))),
    '#default_value' => $team,
    '#parents' => array('teamname', $x, 'name'),
    );

}

function _player_add_form($x, $z, $player = '') {
    
    $form['playername'] = array(
    '#type' => 'textfield',
    '#title' => t('Team @t - Player @p', array('@t' => ($x + 1), '@p' => ($z + 1))),
    '#default_value' => $player,
    '#maxlength' => 50,
    '#parents' => array('teamset', 'pset', 'playername', $z, 'pname'),
    );

}

  return $form;
}

?>
jaypan’s picture

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.

jorje29’s picture

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 ?

jaypan’s picture

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.

jorje29’s picture

Thank you very-very much Jay for your help

jorje29’s picture

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

jaypan’s picture

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.

jorje29’s picture

If I find out something, I will let you know, promise...

jorje29’s picture

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 :

<?php

function mymodule_form($form_state) {

for ($x = 0; $x < $team_count; $x++) {

    #remove team button
    $form['teamset']['remove'] = array(
    '#type' => 'submit',
    '#value' => t('Remove Team @n', array('@n' => ($x + 1))),
    '#submit' => array('team_remove_submit'),
    );

}

}

function team_remove_submit($form, &$form_state) {

    // I do my thing by using $form_state['values']['op']  which has value = Remove Team @n
    $part = split(" ", trim($form_state['values']['op']));
    $button_number = $part[2];
    
}

?>

My code now :

<?php

function mymodule_form($form_state) {

for ($x = 0; $x < $team_count; $x++) {

   #remove team button
    $form['teamset']['remove'] = array(
    '#type' => 'image_button',
    '#src' => 'my_path/remove.png',
    '#value' => t('Remove Team @n', array('@n' => ($x + 1))),
    '#submit' => array('team_remove_submit'),
    );

}

}

function team_remove_submit($form, &$form_state) {
  
    // I do my thing by using $form_state['clicked_button']['#post']['remove']  which has value = Remove Team @n
    $part = split(" ", trim($form_state['clicked_button']['#post']['remove']));
    $button_number = $part[2];
}

?>