-3

Any body know the answer please help me.my code is like this

function my_module_menu() {
    $items = array();
    $items['my_module/form'] = array(
        'title' => t('My form'),
        'page callback' => 'my_module_form',
        'access arguments' => array('access content'),
        'description' => t('My form'),
        'type' => MENU_CALLBACK,
    );
    return $items;
}

function my_module_form() {
    return drupal_get_form('my_module_my_form');
}
function my_module_my_form($form,$form_state) {
    echo "Billing Information";
    echo '<hr>';
    /*$form['checkbox'] = array(
        '#type' => 'checkbox',
        '#title'=> 'HERE',
        '#ajax' => array(
            'callback' => 'checkbox1'
        ),
    );*/
    $form['store'] = array(
        '#type' => 'checkbox',
        '#title' => "use my stored details",
        '#ajax' => array(
            'callback' => 'checkbox1',
            'wrapper'=>'textfields',
        )

    );
    $form['previous'] = array(
        '#type' => 'checkbox',
        //'#disabled'=>'FALSE',
        '#title' => "use my details from the previous page",
    );

    /* $form['name'] = array(
        '#type' => 'fieldset',
        '#title' => t('Name'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
    );*/
    $form['fullname'] = array(
        '#type' => 'textfield',
        '#title' => t('Fullname'),
        '#required' => TRUE,
        '#access'=> TRUE,
        '#description' => "Please enter your full name",
        '#size' => 20,
        '#maxlength' => 255,
    );
    $form['selectbox'] = array(
        '#type' => 'select',
        '#title' => t('Country'),
        '#required' => TRUE,
        '#height' => 20,
        '#maxlength' => 255,
        '#options' => array(
            1 => 'Australia',
            2 => 'Burma',
            3 => 'China',
            4 => 'Denmark',
            5 => 'Egypt',
            6 => 'France',
            7 => 'Geneuva',
            8 => 'Holland',
            9 => 'India',
            10 => 'USA'
        ),
        '#description' => t('Please choose an option.'),
    );
    $form['address1'] = array(
        '#type' => 'textfield',
        '#title' => t('Address1'),
        '#description' => "please enter your Address1",
        '#required' => TRUE,
        '#size' => 20,
        '#maxlength' => 255,
    );
    $form['address2'] = array(
        '#type' => 'textfield',
        '#title' => t('Address2'),
        '#description' => "please enter your Address2",
        '#size' => 20,
        '#maxlength' => 255,
    );
    $form['city'] = array(
        '#type' => 'textfield',
        '#title' => t('City'),
        '#description' => "please enter your City name",
        '#required' => TRUE,
        '#size' => 20,
        '#maxlength' => 255,
    );
    $form['state'] = array(
        '#type' => 'select',
        '#title' => t('state'),
        '#required' => TRUE,
        '#height' => 20,
        '#maxlength' => 255,
        '#options' => array(
            1 => 'Assam',
            2 => 'Bihar',
            3 => 'Chandigar',
            4 => 'Gujarat',
            5 => 'Haryana',
            6 => 'Jammu',
            7 => 'Karnataka',
            8 => 'Maharsatra',
            9 => 'Orissa',
            10 => 'Punjab',
            11 => 'Rajasthan',
            12 => 'Tamilnadu',
            13 => 'Uttrapradesh',
            14 => 'WestBengal'
        ),
    );
    $form['zipcode'] = array(
        '#type' => 'textfield',
        '#title' => t('Zipcode'),
        '#description' => "please enter your Zipcode",
        '#required' => TRUE,
        '#size' => 20,
        '#maxlength'=> 255,
    );
    $form['ponumber'] = array(
        '#type' => 'textfield',
        '#title' => t('PO Number'),
        '#description' => "please enter your PO number",
        '#size' => 20,
        '#maxlength' => 255,
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );
    return $form;
}

function my_module_my_form_validate($form, &$form_state) {
  $year_of_birth = $form_state['values']['year_of_birth'];
  if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
    form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
  }
}

// Adds a submit handler/function to our form to send a successful
// completion message to the screen.

function my_module_my_form_submit($form, &$form_state) {
    drupal_set_message(t('The form has been submitted.'));
}
function checkbox1($form, &$form_state) {
    //print_r($form);
    $form['fullname']['#access']=FALSE;
    //hide($form['fullname']);
    error_log(print_r($form,1));
    return;
}
1
  • Javascript will help
    – Prem Anand
    Commented Jul 3, 2013 at 7:06

2 Answers 2

3

You can use Drupal states, which is probably the easiest and doesn't require you to do any jQuery. Here's an example:

$form['fullname'] = array(
  '#type' => 'textfield', 
  '#title' => t('Fullname'), 
  '#required' => TRUE, 
  '#access'=> TRUE, 
  '#description' => "Please enter your full name", 
  '#size' => 20, 
  '#maxlength' => 255,  
  '#states' => array(
    // Only show this field when the 'store' checkbox is enabled.
    'visible' => array(
    ':input[name="store"]' => array('checked' => TRUE),
    ),
  ),
);
5
  • Ya i need the sample drupal code for that for example my code is like this
    – ulagamuthu
    Commented Jul 3, 2013 at 7:28
  • Look at my answer above for an example.
    – SN_26
    Commented Jul 3, 2013 at 7:36
  • Hi,shabana blackborder Thanks a lot it's working in a effective manner.............
    – ulagamuthu
    Commented Jul 3, 2013 at 8:37
  • If it's working please click on the grey checkbox next to the answer to accept it and reward the person who answered it as well as let the future visitors know that this is the best answer.
    – Beebee
    Commented Jul 3, 2013 at 8:43
  • Instead of using #states i am in need of #access=>false please provide help for me
    – ulagamuthu
    Commented Jul 4, 2013 at 6:10
2

You can also make use of this module WebForm Conditional

0

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