29
Jun
Create custom Modules and Create Custom Form in Drupal
- Category:
- Drupal
Posted On : June 29, 2013
| No Comment
Create Custom Modules Require Two Files
- Custom-Modules-Name.info File
- Custom-Modules-Name.module File
Path :- sites\all\modules\custom\Custom-Modules-Name
Custom-Modules-Name.info File
Code
; $Id$
name = Custom-Modules-Name
description = Custom-Modules Details
core = 6.x
Custom-Modules-Name.module File
Code
<?php
/* Access this Form in Below URL */
URL :- www.xyz.com/node/1
function Custom-Modules-Name_menu() {
$items['node/1'] = array(
‘title’ => ‘Appointment’,
‘page callback’ => ‘Custom-Modules-Name_page’,
‘access arguments’ => array(‘access content’),
);
return $items;
}
// For Menu Callback Page Function
function Custom-Modules-Name_page() {
// Return the HTML generated from the $form data structure.
$out .= drupal_get_form(‘Custom-Modules-Name_form’);
return $out;
}
// Create Custom Form And Fields
function Custom-Modules-Name_form() {
$form['#method'] = ‘POST’;
$form['#name'] = ‘appointment’;
$form['#action'] = ”;
$form['fullname'] = array(
‘#type’ => ‘textfield’,
‘#default_value’=> t(‘Fullname(*)’),
);
$form['fullname']['#attributes']['onfocus'] = “if (this.value == ‘Fullname(*)’) {this.value = ”;}”; $form['fullname']['#attributes']['onblur'] = “if (this.value == ”) {this.value = ‘Fullname(*)’;}”;
$form['mail'] = array(
‘#type’ => ‘textfield’,
‘#default_value’=> t(‘Email(*)’), );
$form['mail']['#attributes']['onfocus'] = “if (this.value == ‘Email(*)’) {this.value = ”;}”;
$form['mail']['#attributes']['onblur'] = “if (this.value == ”) {this.value = ‘Email(*)’;}”;
$form['phone_no'] = array(
‘#type’ => ‘textfield’,
‘#default_value’=> t(‘PhoneNumber(*)’),
);
$form['phone_no']['#attributes']['onfocus'] = “if (this.value == ‘PhoneNumber(*)’) {this.value = ”;}”;
$form['phone_no']['#attributes']['onblur'] = “if (this.value == ”) {this.value = ‘PhoneNumber(*)’;}”;
$date = date(‘Y-m-d’) . ‘ 0000:00:00′;
$format = ‘Y-m-d’;
$form['app_date'] = array(
‘#type’ => ‘date’,
‘#default_value’ => $date,
‘#default_format’ => $format,
);
$form['sel_test_about'] = array(
‘#type’ => ‘textfield’,
‘#default_value’=> t(‘Select Test About(*)’),
);
$form['sel_test_about']['#attributes']['onfocus'] = “if (this.value == ‘Select Test About(*)’)
{this.value = ”;}”;
$form['sel_test_about']['#attributes']['onblur'] = “if (this.value == ”)
{this.value = ‘Select Test About(*)’;}”;
$form['submit'] = array(
‘#title’ => t(‘Submit’),
‘#type’ => ‘submit’,
‘#value’ => ‘Submit’,
‘#submit’ => array(‘Custom-Modules-Name_form_submit’),
);
$form['view'] = array(
‘#type’ => ‘markup’,
‘#value’ => l(t(‘view’), ‘appointment’ ),
);
return $form; }
// Form_Submit
function Custom-Modules-Name_form_submit($form, &$form_state) {
$date_time = date(“y-m-d h:i:s”, time());
$query=db_query(“INSERT INTO `drupal628`.`appointment`(`fullname`, `mail`, `phone_no`,`select_test`, `app_date`) VALUES (‘%s’, ‘%s’,'%s’,'%s’,'$date_time’)”,
$form_state['values']['fullname'],
$form_state['values']['mail'],
$form_state['values']['phone_no'],
$form_state['values']['sel_test_about'],
$form_state['values']['app_date']
);
drupal_set_message(t(‘Your Form has been saved.’)); }
// Form_Validate
function Custom-Modules-Name_form_validate(&$form,&$form_state) {
if($form_state['values']['fullname'] == ” || $form_state['values']['fullname'] == ‘Fullname(*)’) {
form_set_error(‘fullname’, t(‘Please enter Fullname.’));
}
if($form_state['values']['mail'] == ” || $form_state['values']['mail'] == ‘Email(*)’) {
form_set_error(‘mail’, t(‘Please enter Email.’));
}
if($form_state['values']['phone_no'] == ” || $form_state['values']['phone_no'] == ‘PhoneNumber(*)’) {
form_set_error(‘phone_no’, t(‘Please enter Phone.’));
}
if($form_state['values']['sel_test_about'] == ” || $form_state['values']['sel_test_about'] == ‘Select Test About(*)’) {
form_set_error(‘sel_test_about’, t(‘Please enter Select Test About.’));
}
}
Screenshot
- Tags: