3

I'm creating my very first Drupal module (Drupal 7, btw) and I'm almost done with it. There is one last thing and that is that I need to get the values from multiple checkboxes.

My module list certain nodes and each of them gets a checkbox using a foreach $nid. All using the Form API.

Now, when the form is submitted, only one (1) value is returned - and as of now, not even the one that I check, but the last nodes ID.

Perhaps it is because each new value replaces the previous(?). Or something is wring with my code. Someone in the IRC channel told be that the forms checkbox returns an array of the values, but I'm trying to use foreach of that value, without any success.

What could be the problem? Here is my code - shorted down.

function my_module_cmp($a, $b) {

  $a = (array) $a;

  $b = (array) $b;

  return strcmp($a['name'], $b['name']);

}

function my_module_form() {
$url = taxonomy_get_term_by_name(arg(1));
foreach($url as $term) {
    $tid = $term->tid;
}
$term = taxonomy_term_load($tid);
$name = taxonomy_term_title($term);
$terms = taxonomy_get_tree(3,0,1);
usort($terms, "my_module_cmp");
$counter = 0;
$result = taxonomy_select_nodes($tid);
foreach($result as $nid) {
    $form[$nid] = array (
        '#type' => 'fieldset'
    );
    // title
    $form[$nid]['title'] = array (
        '#markup' => '<div class="title"><a href="/'.$nid.'">' . $node->title . '</a></div>',
    );
    // checkbox
    $form[$nid]['company'] = array (
        '#type' => 'checkbox',
        '#title' => t('Check the nodes balbla'),
        '#title_display' => 'attribute',
        '#return_value' => $nid,
        '#default_value' => 0,
        '#prefix' => '<a class="checkbox">Skicka förfrågan till företaget >>',
        '#suffix' => '</a>',
    );
$counter++;
}
return $form;
}

    function my_module_form_validate($form, &$form_state) {
// Validate that a company has been checked, at all
$valid_company = $form_state['values']['company'];
    if (!$valid_company) {
    form_set_error('company', 'Forgot to check something');
    }
}

function my_module_form_submit($form, &$form_state) {
$company = $form_state['values']['company'];
drupal_set_message('<pre>'.print_r($form_state['values'], 1).'</pre>'); // Check out the values

foreach ($company as $nid) {
    // Get the node from nid
    $node = node_load($nid);
    // Get the author of that node
    $user = user_load(array('uid' => $node->uid));
    drupal_set_message(t('DEBUG node:'.$nid.', user: '.$user->name.','.$user->mail.'')); // TEST



    // Check if the mail has been sent and show a message based on that
    if (drupal_mail('my_module', 'token', $user->mail, language_default(), $message, $from, TRUE)) {
        drupal_set_message(t('Offert been sent %name',array('%name' => $user->name)));
    }
    else {
        drupal_set_message(t('Error')); 
    }
} 
}
2
  • There is problems in your code, syntax & logic. #1 where are you getting $node->title from? #2 $form[$nid]['company']['#prefix'] should be '<a class="checkbox">'.t('Skicka forfragan till foretaget'). #3 A checkbox that is a link? #4 I don't think you need $counter++, believe you can do count($form) or something. $5 I might screw thinks up but try $form['fieldset'][#type] = 'fieldset', $form['fieldset'][$nid][#type] = 'item', $form['fieldset'][$nid]['company'][#type] = 'checkbox'.
    – iStryker
    Jul 12, 2011 at 17:14
  • also, for debugging, install the devel module, and use dsm() instead of drupal_set_message. It will display an array with a nice UI
    – iStryker
    Jul 12, 2011 at 17:16

1 Answer 1

1

Your problem is that you have named the chechboxes the same thing which is why you only get one value.

You could replace ['company'] with [$nid] or use '#tree' => TRUE on the field set to make the values turn into a nested array.

4
  • The tree didn't work, but then $nid worked. But then the issue is how do I get that value in the submit function when I dont know the $nid in that function? Jul 12, 2011 at 19:56
  • Actually the three did work. But I still get an array that I dont know how to get the values from: [509] => Array ( [companies] => 509 ) [508] => Array ( [companies] => 508 ) Jul 12, 2011 at 21:06
  • @Daniel getting your values is a simple case of using foreach. But that is hardly a Drupal question.
    – googletorp
    Jul 12, 2011 at 22:06
  • That is probably true, but I still dont know how I get that array into a variable to use foreach. Jul 12, 2011 at 22:27

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.