6

Is there anyway to set the default values for checkboxes? What im trying to do is set a specific value for each checkbox so it will delete the selected row in my db, however table select seems to just go off of its own value instead of the one I am trying to assign them with.

    function auto_form_records($form, &$form_state) {
//Table header
    $header = array(
        '',
        'First Name', 
        'Last Name', 
    );

//DB query
    $dbresult = db_query("SELECT * FROM {auto}");

        foreach ($dbresult as $dbrow) {

            $select_id[] = array(
            $dbrow->id
            );
            $rows[] = array(
            $dbrow->firstname,
            $dbrow->lastname,
            );
        }

//Table select Form     
    $form['checkboxes'] = array(
        '#type' => 'tableselect',
        '#header' => $header,
        '#options' => $rows,
        '#empty' => 'No Records were found.',
        '#default_value' => $select_id
    );

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

//Delete row in db.   
    function auto_form_delete_row($form, &$form_state){
        $id = $form_state['values']['checkboxes'];
        db_delete('autoleads')
      ->condition('id', $id)
      ->execute();
    }

//Submission    
    function auto_form_records_submit($form, &$form_state){
        $data = drupal_set_message(t(dvm($form_state)));
        //auto_form_delete_row($form, $form_state);
        return $data;
    }

I would think that me passing the string into default_value would atleast give me the values I am trying to pass through, but its not...

Any suggestions is appreciated.

8
+50

You just need to add your id as the key for each item in your $rows array, e.g.:

foreach ($dbresult as $dbrow) {
  $rows[$dbrow->id] = array(
    $dbrow->firstname,
    $dbrow->lastname,
  );
}

Then in your submit handler run:

// Limit the checkbox ids to only those selected by the user
$selected = array_filter($form_state['values']['checkboxes']);

db_delete('autoleads')
  ->condition('id', $selected, 'IN')
  ->execute();

And you should be good to go :)

There's no need to use #default_value unless you want to pre-select some of the checkboxes for the user. If you do, just set #default_value to an array of the ids that you want selected initially.

  • Cant believe I didnt try that, just one question, im looking at your condition example and I see the argument 'IN' can you explain what this argument is? Your example works fine I just want a full understanding of the function. Thanks – Jeremy Love Oct 15 '12 at 17:15
  • It's literally the MySQL operator IN...when you use that operator with an array as the value Drupal (maybe PDO I'm not sure) converts it to col IN (1, 2, 3). It's just a handy shortcut so you don't need to write that code yourself, and the arguments are sanitised as you'd expect – Clive Oct 15 '12 at 17:19
  • ahh ok, I kinda figured it may be that but wasnt sure. Thank you again for your help and advice. – Jeremy Love Oct 15 '12 at 18:16

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.