Question

I am new to Drupal. I have created a contace1 module with the following code:

contace1.info

; $Id$
name = Contact1
description = Show how to build contact form
package = Pro Drupal Development
core = 6.x

contact1.module

// $Id$

/**
* @file
* practice to build form
*/

/**
* Implimentation of hook_menue().
*/

function contact_menu()
    {
        $items['contact1'] = array(
            'title' => 'Contact',
            'page callback' => 'contact_page',
            'access argument' => array('access content'),       
            'type'=>MENU_CALL_BACK,
            'access callback' => TRUE,

            );
            return $items;

    }

/**
* menu callback
* called when user goes to http://localhost/drupaldemo/?q=contact
*/

function contact_page()
    {
        $output = t('You can leave a message using the contact form below.');
        //Return the html generated from $form data structure.
        $output.= drupal_get_form('contact_nameform');
        return $output;
    }
    /**
    * define the form
    */
function contact_nameform()
    {
        $form['user_name']= array(
        '#title' =>t('Your Name'),
        '#type' => 'textfield',
        '#description' => t('Please enter your name.'),
                );
        $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
        )       ;
        return $form;
    }

/**
* validate the form
**/
function contact_nameform_validate()
    {
        if($form_state['values']['user_name']=="")
            {
            form_set_error('user_name',t('Please enter your name.'));
            }

    }

/**
* handle post validation form submition
*/
function contact_nameform_submit($form ,&$form_state)
    {
    $name=$form_state['values']['user_name'];
    drupal_set_message(t('Thanks for filling out the form, %name',array('%name'=>$name)));

    }

im this code i have tried to create new contact form

but it does not show any link and on opening page directly give page not found.

No correct solution

OTHER TIPS

First of all, MENU_CALL_BACK does not defined in Drupal. What you wanted to write is MENU_CALLBACK, which registers a menu item in the menu router. This item won't appear in any visible menu normally. Think of it as a hidden menu item. If you want to make it visible, use MENU_NORMAL_ITEM.

'type' = MENU_CALL_BACK - menu is callback, you should set it to MENU_NORMAL_ITEM or manually create menu in admin page to contact1 page. Refresh cache.

I recommend to you fully read "Pro Drupal Development" from Vandyk, there's examples how to create forms :)

The first error in the code is that, if the module is named contact1.module, then every hook it implements should have a name starting with contact1_. You should then avoid using contact_ in the name of your module's functions, as there is already the Contact module in Drupal 6; in the case your module is for Drupal 6, there would be a conflict between the modules.

The second error is that the constant you are using is MENU_CALLBACK, not MENU_CALL_BACK.

If then contact1.module is the name of your module, the info file that comes with it should be named contact1.info, not contace1.info. If you use a wrong name for that file, Drupal 6 and higher should not show your module in the list of the modules you can install.

Hi try to use this code

/**
* menu callback
*
*/

function contact_page()
    {
        $output = array(
            'item 1' => array(
              "#type" => 'markup',
              '#markup' =>  t('You can leave a message using the contact form below.'),
            ),
            'item 2' => array(
              "#type" => 'markup',
              '#markup' =>  drupal_get_form('contact_nameform'),
            ),
        );
        return $output;
    }
    /**
    * define the form
    */
function contact_nameform($form, $form_state)
    {  ..........
       .......

Here,I set markup type to return output with content and form, also set the parameter of contact_nameform($form, $form_state)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top