2

I have a table of information displayed on a page. Each row of this table has its own submit button. I need to be able to pass information depending on what submit button is pressed.

The form is built like

function numberSubmit_form ($form, &$from_submit){
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
    return $form;
}

The rows of the table are built like:

$unvalRows[] = array(
            array('data' => '0'.substr($a[0], 2)),
            array('data' => drupal_render(drupal_get_form('numberSubmit_form'))),
        );

How am I able to determine which submit button has been used and pass the applicable phone number?

2 Answers 2

2

Add the phone number as an argument to numberSubmit_form and include it in your form array:

$unvalRows[] = array(
    array('data' => '0'.substr($a[0], 2)),
    array('data' => drupal_render(drupal_get_form('numberSubmit_form', $phone_number))),
);

Then in the form constructor:

function numberSubmit_form ($form, &$from_submit, $phone_number){
    $form['phone_number'] = array(
        '#type' => 'hidden',
        '#value' => $phone_number,
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
    return $form;
}

It will then be available to you in the submit handler:

function numberSubmit_form_submit(&$form, &$form_state) {
    // do something with $form_state['values']['phone_number']
}
2
  • You are right, @dooffas's approach was wrong, as he wanted to get the submit button's data... But I think using radio buttons in a form would be even easier.
    – Sk8erPeter
    Commented Apr 17, 2012 at 16:57
  • It is working fine. But i want to use '#ajax' instead of normal form submit. IF i have tried '#ajax', but it is not working.
    – Guru
    Commented Jul 28, 2015 at 4:40
1

Same answer as charlie except that #value didn't work for me; I had to put #default_value in the hidden field as follow:

$unvalRows[] = array(
    array('data' => '0'.substr($a[0], 2)),
    array('data' => drupal_render(drupal_get_form('numberSubmit_form', $phone_number))),
);


function numberSubmit_form ($form, &$from_submit, $phone_number){
    $form['phone_number'] = array(
        '#type' => 'hidden',
        '#default_value' => $phone_number,
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
    return $form;
}

function numberSubmit_form_submit(&$form, &$form_state) {
    // do something with $form_state['values']['phone_number']
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.