Hi all,
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'));
}
}
}
Comments
At the moment you're checking
At the moment you're checking for $form_state['values']['company'], but you have never set an element called $form['company'] in the form function, so that value won't exist. Also you're outputting $node->title, when the $node object has not been set in your code anywhere.
You can do what your trying to do with your current code but I'd suggest changing it to use the checkboxes form type instead:
In your form function:
and in your submit function:
Hope that helps
EDIT
----
One other little thing, your form function should have the required parameters for consistency:
My guess is you need to
My guess is you need to change the fieldset to
That way the elements will in the fieldset will retain the node id in the value structure. Another approach would be to use a single set of checkboxes, something like
Thank guys,I'm trying out
Thanks guys,
I'm trying out both of your tips but I cant figure it out completely. I get as far as multiple values are shown with:
drupal_set_message('<pre>'.print_r($form_state['values'], 1).'</pre>');Looks like:
[509] => Array
(
[companies] => 509
)
[508] => Array
(
[companies] => 508
)
How do I get into that value now? I cant use:
$nids = $form_state['values']['companies'];I would have to get the id somehow or is there another way?
See the first
See the first reply:
if you're using the multiple checkbox method. If you're using the #tree method then I'd suggest wrapping all your values in another wrapper in the form so you can easily walk through them in your submit function.
then in your submit function you can just do this:
Thank you,I got it working
Thank you,
Now I get the nids, but I get them even if I havent checked the checkboxes?
Yep, you need to check the
Yep, you need to check the 'companies' value of each array item to see if it's the nid or zero (zero means unchecked). It would be a lot easier to do it using the checkboxes rather than using #tree.
I dont know how to do that so
I dont know how to do that so I changed to checkboxes instead. Now an extra checkbox is added with the name $nid--2 and if check the node with that extra checkbox, the form fails and asks me to contact admin.
It definitely shouldn't be
It definitely shouldn't be adding an extra checkbox if you've used the code from either of the examples above, could you post your current code? I'm sure it'll be something simple
Ok, now there isnt an extra
Ok, now there isnt an extra checkbox anymore, but I can only check one box, on my dev page.
When using checkboxes the
When using checkboxes the options array needs to be build first so it would have to go after the loop that builds $options and does not need/want $nid so it would look like
Wont that mean that there
Wont that mean that there wont be a checkbox for each entity? They have to be together with the node.
checkbox values retrieval method
Hi Daniel,
Hope this helps.
https://drupal.stackexchange.com/a/185911/56061