Saturday 20 July 2013

Drupal custom registration form with Add/Edit/Delete functionality

This is Drupal customer registration form. how to create a custom form which we have already discussed in previous post, Now we will create custom customer registration form with drupal.

First we will create a custom page and create customer registration form in this, you can see below picture and code also.



 




In customer.mobule 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['customer'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Customer List', //page title
    'description' => 'A form to mess around with.',
    'page callback' => 'form_customer_form', //this is the function that will be called when the page is accessed.for a form, use drupal_get_form
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
 
 
  return $items;
 
}
function form_example_form($form, &$form_state) {

 if(arg(1)){
  $data = db_select('customer_tbl', 'da')
   ->fields('da', array('first_name', 'last_name', 'mobile', 'email', 'id'))
   ->condition('id' , arg(1))
   ->orderBy('first_name', 'ASC') //Most recent first.
   ->execute()
   ->fetchAll();
 }else{
  $data[0]->first_name="";
  $data[0]->last_name="";
  $data[0]->mobile="";
  $data[0]->email="";
  $data[0]->id="";
 }

  $form['first_name'] = array(
    '#type' => 'textfield', //you can find a list of available types in the form api
    '#title' => 'First name',
    '#size' => 50,
    '#maxlength' => 50,
 '#value' => $data[0]->first_name,
    '#required' => TRUE, //make this field required
  );
  $form['last_name'] = array(
    '#type' => 'textfield', //you can find a list of available types in the form api
    '#title' => 'Last name',
    '#size' => 50,
    '#maxlength' => 50,
 '#value' => $data[0]->last_name,
    '#required' => TRUE, //make this field required
  );
  $form['mobile'] = array(
    '#type' => 'textfield', //you can find a list of available types in the form api
    '#title' => 'Mobile',
    '#size' => 50,
    '#maxlength' => 10,
 '#value' => $data[0]->mobile,
    '#required' => TRUE, //make this field required
  );
 
  $form['email'] = array(
    '#type' => 'textfield', //you can find a list of available types in the form api
    '#title' => 'Email-address',
 '#size' => 50,
 '#value' => $data[0]->email,
    '#required' => TRUE, //make this field required
  );
  
  $form['cust_id'] = array(
    '#type' => 'hidden', //you can find a list of available types in the form api
 '#value' => $data[0]->id,
  );
  
  $form['submit_button'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  
  
  return $form;
}
function form_example_form_validate($form, &$form_state) {

 if(!filter_var($form_state['values']['email'], FILTER_VALIDATE_EMAIL)){
   form_set_error('email', t('Invalid Email Address'));
 }
}


URL demo is for registration form, After submit this form it will go to validate function for validate all fields.
this is server side validation from drupal side.


function form_example_form_submit($form, &$form_state) {
 //print_r($form_state);exit;
 $values['cust_id'] = $form_state['input']['cust_id'];
 if($values['cust_id']){
  $values = array(
   'first_name' => $form_state['input']['first_name'],
   'last_name' => $form_state['input']['last_name'],
   'mobile' => $form_state['input']['mobile'],
   'email' => $form_state['input']['email'],
   'cust_id' => $form_state['input']['cust_id'],
  );
  
  $update = db_update('customer_tbl')
      -> fields(array(
     'first_name' => $values['first_name'],
     'last_name' => $values['last_name'],
     'mobile' => $values['mobile'],
     'email' => $values['email'],
     ))
      ->condition('id', $values['cust_id'])
      ->execute();
 }else{
  $values = array(
  'first_name' => $form_state['input']['first_name'],
  'last_name' => $form_state['input']['last_name'],
  'mobile' => $form_state['input']['mobile'],
  'email' => $form_state['input']['email'],
  );
  
  $insert = db_insert('customer_tbl')
   -> fields(array(
    'first_name' => $values['first_name'],
    'last_name' => $values['last_name'],
    'mobile' => $values['mobile'],
    'email' => $values['email'],
   ))
   ->execute();
 }
    drupal_set_message(t('Customer Details have been saved'));
    $form_state['redirect'] = url('customer', array('absolute' => true));
     
 
}



After submit and validate,  it will come submit function(Above),  here we are saving all field data in to database. customer_tbl is a table.



function form_customer_form() {
 

 if(arg(1)){
  $data = db_delete('customer_tbl')
   ->condition('id', arg(1))
   ->execute();
 }
 $data = db_select('customer_tbl', 'da')
    ->fields('da', array('first_name', 'last_name', 'mobile', 'email', 'id'))
    ->orderBy('first_name', 'ASC') //Most recent first.
    ->execute()
 ->fetchAll();
 
 $output = '';
 global $base_url;
 $homeout = '';
 $homeout = '';
 $homeout .= '
Add Customer | Delete Customer
'; for($i=0;$i
First Name Last Name Mobile Email

'.$data[$i]->first_name.'
'.$data[$i]->last_name.'
'.$data[$i]->mobile.'
'.$data[$i]->email.'
'; } $homeout .= ' '; $output .=$homeout; return $output; }  

 This Customer List page where all customer details is showing, In this function there are 2 links.     
1) Add customer  
2) Delete customer    

When you click on Add customer. this link will bring you on customer form and if you checked any name radio button and click on delete customer then this customer details will be delete. If you want to edit any customer details then you have to click on customer name, it will bring you on customer form with all filled details. there you can edit details and save.

   

  For customer.module file Please Download from here.  

 Enjoy Guys and If any issue you can comment here.

No comments:

Post a Comment