0

I want a list of the input of my saved database table with buttons to delete or view the content and want to redirect it in the submit function.

With this code snipped i get only the submitted input value of the buttons, but every time is the $form_state['values'] array filled with the 1st form values. Is that the right way or need I another solution?

function myform_block_view($delta = '') {
    $block = array();

    global $user;
    $uid = $user->uid;
    $utime = $user->timezone;

    $query = db_select('mytable', 'm');
    $query->condition('m.uid', $uid, '=');
    $query->fields('m', array('fid', 'uid', 'name', 'created'));
    $query->range(0, 50);

    $results = $query->execute()->fetchAll();

    if($uid) {  
        switch ($delta) {
            case 'myblock':
                $block['subject'] = '<none>';
                $block['content'] = _block_content($results);
            break;
        };
    };  
    return $block;
}

function _block_content($items) {
    $output = array();

    foreach($items as $item => $item_value) {
        $output[$item] = drupal_get_form('mymodule_form', $item_value);
    };
    return $output;
}

function mymodule_form($form, &$form_state, $i) {

    global $user;
    $uid = $user->uid;
    $utime = $user->timezone;

        $form['value-'.$i->fid] = array( //EDIT
            '#type' => 'value',
            '#value' => $i->fid,
        );
        $form['view-'.$i->fid] = array ( //EDIT
            '#type' => 'submit',
            '#value' => t('view'),
            '#name' => 'view-'.$i->fid,
        );
        $form['delete-'.$i->fid] = array ( //EDIT
            '#type' => 'submit',
            '#value' => t('delete'),
            '#name' => 'delete-'.$i->fid,
        );

    return $form;
}

function mymodule_form_submit($form, &$form_state) {
   dpm($form_state);
   $form_state['rebuild'] = TRUE; //EDIT
}

The dpm output without 'form_build_id' 'form_token' 'form_id' in both, 1st example:

input =>  
    view-3 => view
    value3 (NULL)

values =>     
    value2
    view-2 => view
    delete-2 => delete

2nd example:

input =>  
    view-3 => view
    value3 (NULL)

values =>     
    value3
    view-3 => view
    delete-3 => delete
  • can you add the result of the dpm? – longboardnode Dec 20 '16 at 4:17
  • I write one of the dpm after the changes of Yogesh. This is the one wich matched with the submit input. – Bruno Dec 20 '16 at 12:45
  • This dpm makes no sense... can you check that it's most recent after your updates to the code? – longboardnode Dec 20 '16 at 13:58
  • That`s what i get. I add a 2nd dpm output in my question from one submit. – Bruno Dec 20 '16 at 14:09
  • Can you add a dpm of $i in mymodule_form? – longboardnode Dec 20 '16 at 16:20
1

It should be because the way you're generating the form is wrong, form element should be as below:

<?php
function mymodule_form($form, &$form_state, $i) {
  $form['value_' . $i->fid] = array(
    '#type' => 'value',
    '#value' => $i->fid,
  );
  $form['view_' . $i->fid] = array (
    '#type' => 'submit',
    '#value' => t('view'),
    '#name' => 'view_' . $i->fid,
  );
  $form['delete_' . $i->fid] = array (
    '#type' => 'submit',
    '#value' => t('delete'),
    '#name' => 'delete_' . $i->fid,
  );

  return $form;
}
?>

Each element name in $form array should be unique. Check Drupal Form API reference guide for more details. The value, view and delete doesn't contain any form element data like #type, etc. Please check the value of $form_state['values']['op'] in your form submit handler to understand which one is called.

  • Ah, you have right! Now I can compare the 'input' with the 'values' in the $form_state array. I forgot the $form_state['rebuild'] = TRUE; in the submit function too. The submit is calling for every loop. I havnt any 'op' in 'values', also the value doesnt help, it returns only the value of the current loop in 'input'. Is there not a better solution? – Bruno Dec 20 '16 at 12:25
  • In submit handler you can check for $form_state['values']['op'] which will give you which submit button is clicked, suppose for e.g. delete_2 so you'll have that to identify. – Yogesh Dec 20 '16 at 12:41
  • Really I havn`t any 'op' in $form_state['values'], the values changes all with the fid in the loop. Only the 'input' displays me the clicked button. I can write two of the dpm outputs if you want. – Bruno Dec 20 '16 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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