Ron
Ron

Reputation: 394

How to make checkbox checked and unchecked

In my form I have a field which uses checkbox , and the checkbox is not a mandatory field

$form['ishot'] = array(
        '#type' => 'checkbox', //you can find a list of available types in the form api
        '#title' => 'Is Hot',
        '#prefix' => "<label for='edit-ishot'>Is Hot <span class='form-required' title='This field is required.'>*</span></label>",
        '#required' => TRUE, //make this field required
        );

now if i put

'#default_value' => 1

the checkbox is checked and whenever I unchecked it it throws a notice

Notice: Undefined index: ishot in submit_admin_news_add()

Also i want my checkbox will be unchecked by default and user could submit the form without checking it .

This is the total function

function submit_admin_news_add($form, &$form_state)
{


    //echo "<pre>";
    //print_r( $form_state['input'] );
    //echo "</pre>";
    //die();

    $title= $form_state['input']['title'];
    $subtitle= $form_state['input']['subtitle'];
    $tags= $form_state['input']['tags'];
    $category= $form_state['input']['category'];
    $source= $form_state['input']['source'];
    $status= $form_state['input']['status'];
    $newstype= $form_state['input']['newstype'];

    if( array_key_exists("ishot", $form_state['input']) )
    {
        $ishot= $form_state['input']['ishot'];      
    }
    else
    {
        $ishot= 'N';
    }
    if( array_key_exists("sendque", $form_state['input']) )
    {
        $sendque= $form_state['input']['sendque'];
    }
    else
    {
        $sendque= 0;
    }
    if( array_key_exists("immedialecron", $form_state['input']) )
    {
        $immedialecron= $form_state['input']['immedialecron'];
    }
    else
    {
        $immedialecron= 0;
    }




    $introduction= $form_state['input']['introduction'];
    $fullstory= $form_state['input']['fullstory'];
    $extralink= $form_state['input']['extralink'];

    //date creation
    $date_raw   = $form_state['input']['dateadded'];
    $o_date_raw = $date_raw['date'];
    $o_time_raw = $date_raw['time'];

    $exp_date = explode('/',$o_date_raw);

    $new_date = $exp_date[2].'-'.$exp_date[0].'-'.$exp_date[1];

    $new_date_time = $new_date.' '.$o_time_raw.':00';

    //$new_date_time;
    //date ceration end


    if( array_key_exists("additionalcat", $form_state['input']) )
    {
        $additional = $form_state['input']['additionalcat'];
    }
    else
    {
        $additional = '';
    }

    if( array_key_exists("compcataccess", $form_state['input']) )
    {
        $comp_access = $form_state['input']['compcataccess'];
    }
    else
    {
        $comp_access = '';
    }


    $validators = array();
    if($file = file_save_upload('file', $validators,"public://",FILE_EXISTS_RENAME))
    {
        //print_r( $file );

        $new_file_name_raw = $file-> uri;
        $new_file_name     = str_replace("public://","",$new_file_name_raw);


        drupal_set_message('succeeded', 'File successfully uploaded.');
    }
    else {
        $new_file_name = '';
        form_set_error('failed', 'File was not uploaded.');
    }


    $query = db_insert('custom_news_management')->fields(array('title','subtitle','tags','category_id','source','status','newstype','ishot','sendque','immedialecron','introstory','fullstory','extralink','date_added'));
    $query->values(array(
        'title' => $title,
        'subtitle' => $subtitle,
        'tags' => $tags,
        'category_id' => $category,
        'source' => $source,    
        'status' => $status,
        'newstype' => $newstype,
        'ishot' => $ishot,
        'sendque' => $sendque,
        'immedialecron' => $immedialecron,
        'introstory' => $introduction,
        'fullstory' => $fullstory,
        'extralink' => $extralink,
        'date_added' => $new_date_time,
        ));

    $last_id =  $query->execute();

    if( $new_file_name != '' )
    {
        $insert_file = db_insert('news_file')->fields(array('news_id','file'));
        $insert_file->values(array(
            'news_id' => $last_id,
            'file' => $new_file_name,
            ));
        $insert_file->execute();        
    }
    if( $additional != '' )
    {
        foreach( $additional as $val )
        {
        $insert_additional = db_insert('news_additional_category')->fields(array('news_id','category_id'));
        $insert_additional->values(array(
            'news_id' => $last_id,
            'category_id' => $val,
            ));
        $insert_additional->execute();  
        }
    }

    if( $comp_access != '' )
    {
        foreach( $comp_access as $value )
        {
        $insert_comp_access = db_insert('news_company_access')->fields(array('news_id','category_id'));
        $insert_comp_access->values(array(
            'news_id' => $last_id,
            'category_id' => $value,
            ));
        $insert_comp_access->execute(); 
        }   
    }


    drupal_set_message(t('News inserted successfully!'), 'success');
}

Upvotes: 1

Views: 10062

Answers (2)

Allena
Allena

Reputation: 53

Though very late to reply but for information, you should use
'#default_value' => variable_get('ishot', 1); //checked by default

instead of using '#default_value' => 1

Upvotes: 1

szikael
szikael

Reputation: 374

"Also i want my checkbox will be unchecked by default" add:

'#default_value' => 0,

I would suggest to do some tests like:

if( array_key_exists("ishot", $form_state['input']) )
    {
        $ishot= 'Defined';
        drupal_set_message(t('Printing $form_state:  '.$form_state['input']['ishot']));      
    }
    else
    {
        $ishot= 'Not Defined';
    }
drupal_set_message(t($ishot));
//or dpm($ishot);

I think, even if user did't mark checkbox $form_state['input']['ishot'] may exist (not so sure). I would try also to do:

    $query = db_insert('custom_news_management')
            ->fields(array(
                     'title' => $title,
                     'subtitle' => $subtitle,
                     'tags' => $tags,
                     'category_id' => $category,
                     'source' => $source,    
                     'status' => $status,
                     'newstype' => $newstype,
                     'ishot' => $ishot,
                     'sendque' => $sendque,
                     'immedialecron' => $immedialecron,
                     'introstory' => $introduction,
                     'fullstory' => $fullstory,
                     'extralink' => $extralink,
                     'date_added' => $new_date_time,
                     ));  
   $last_id =  $query->execute();

Your approach looks like :http://dropbucket.org/node/111 but a bit modified.

 $query = db_insert('custom_news_management')->fields(array('title',...'ishot',..));
 $query->values(array(.....
        ));

I hope that solve the problem.

Upvotes: 1

Related Questions