0

I have module, that should build form. However it is not showing form.

There is my code:

function weblabs_form() {
    $form = array();

$form['name']=array(
    '#type'=>'textfield',
    '#title'=>t('Enter your name'),
    '#description'=>t('Your first name goes here')
);
$form['last_name']=array(
    '#type'=>'textfield',
    '#title'=>t('Enter your Last name'),
    '#description'=>t('Your Last name goes here')
);

$form['email']=array(
    '#type'=>'textfield',
    '#title'=>t('Enter your email'),
    '#description'=>t('Your email goes here')
);

$form['country']=array(
    '#type'=>'select',


       '#title'=>t('Select your country'),
        '#options'=>array('USA','UK','France','Japan')
    );

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

    return $form;
}

function weblabs_form_submit($form, &$form_state){
    echo("<h1>TEST</h1>");
    drupal_set_message("TEST");
}

function weblabs_menu() {
    $items['calculator'] = array(
        'title' => 'Kalkulačka WebLabs',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('weblabs_form'),
        'access arguments' => array('access content'),
        'type' => MENU_CALLBACK,
        );
    return $items;
}`
2

To print the HTML code corresponding to your form you need to render it:

$form = drupal_get_form('weblabs_form');
print drupal_render($form);
|improve this answer|||||
0

Maybe the name (id) of your form is not the most appropriate : weblabs_form reference the hook_form hook, with is used to add custom form elements to be displayed in the node editing form.

Consider changing its name (and the submit method associate) with something less ambivalent (example: calculator_page and calculator_page_submit).

The drupal_get_form method returns a Form API array for a given form ID. You must use the drupal_render() method to render it properly... See https://www.drupal.org/node/224333#unrendered.

After, the MENU_CALLBACK option do not generate a menu, but you must be able to access your form on /calculator on your drupal website. The drupal_render method is directly call from the template (normally).

|improve this answer|||||
0

In addition, if you want to have a form page. You should use drupal_get_form as the page call function and the form builder name as the arguments

function mymodule_menu() {
  $items['path/to/form'] = array(
    'title' => 'Weblabs Form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('weblabs_form'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}
|improve this answer|||||

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.