Tuesday 16 July 2013

Drupal 7 create custom form

For Drupal custom form, we have to write hook function for creating the url first.

Please see below code which will help you for writing custom form.

In demo.module file

function demo_menu(){

$items = array();

  $items['demo'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Customer Registration Form', //page title
    'description' => 'A form to mess around with.',
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.for a form, use drupal_get_form
    'page arguments' => array('form_example_form'), //put the name of the form here
    'access callback' => TRUE
  );

$items['success'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Customer Registration successfully', //page title
    'description' => 'A form to mess around with.',
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.for a form, use drupal_get_form
    'page arguments' => array('form_success_form'), //put the name of the form here
    'access callback' => TRUE
  );


  return $items;

}

function form_success_form() {

$form['msg'] = array('#title' => 'Customer Registration successfully');

return $form;
}
/* this code (noted in the callback above) creates the
* contents of the "demo" page */

function form_example_form($form, &$form_state) {
 
  $form['name'] = array(
    '#type' => 'textfield', //you can find a list of available types in the form api
    '#title' => 'Your name',
    '#size' => 50,
    '#maxlength' => 50,
    '#required' => TRUE, //make this field required
  );

  $form['comment'] = array(
    '#type' => 'textarea', //you can find a list of available types in the form api
    '#title' => 'Comment',
    '#required' => TRUE, //make this field required
  );

  $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
 
  return $form;
}
function form_example_form_validate($form, &$form_state) {
}

function form_example_form_submit($form, &$form_state) {
	$values = array(
		'name' => $form_state['values']['name'],
		'comment' => $form_state['values']['comment'],
	);

	$insert = db_insert('customer_tbl')
		-> fields(array(
			'name' => $values['name'],
			'comment' => $values['comment'],
		))
		->execute();
	drupal_set_message(t('Customer Details have been saved'));
	$form_state['redirect'] = url('success', array('absolute' => true));
	

}
 
Put this code in your example.module file
Here "demo_menu()" is a hook function, according to that it will create url for page and "form_example_form" is a form where all text field and submit button define.

After submit this form with filling, it will go to "form_example_form_validate" and will check validation then

it will go through "form_example_form_submit". in this function we are saving all values in the database.

Note: we have to create 1 table in database manually for this custom form  and have to create field also in database then this values will save after submit.


$form_state['redirect'] is a form redirection after submit the form.


No comments:

Post a Comment