Monday 22 July 2013

client side validation drupal 7

Drupal provides client-side validation by module. If you install this module in your project then in whole project client side validation will work without reloading page.

So all steps are given below, Please follow below steps.

Step 1:

The main module (Clientside Validation) provides the core functionality for Clientside Validation. However, it does nothing by itself, it needs at least one of its submodules to provide any real functionality. The submodules are:
  • Clientside Validation FAPI: Provides Clientside Validation for the Form API Validationmodule.
  • Clientside Validation Field Validation: Provides Clientside Validation for the Field validationmodule (Drupal 7 only).
  • Clientside Validation Form: Provides Clientside Validation for standard Form API elements.
  • Clientside Validation HTML5: Provides Clientside Validation for HTML5 elements (seeDrupal HTML5 Initiative, Drupal 7 only).
  • Clientside Validation Webform: Provides Clientside Validation for the Webform andWebform Validation module.
 Here you can download all submodule related to this client side validation and download Clientside Validation.

at the time of Installation this module below screen will come.




To configure settings for this module go to admin/config/validation/clientside_validation for Drupal 7 or to admin/settings/clientside_validation for Drupal 6. There are several settings for this module, we will guide you through them.

Step 2: 

Go through this path  "Home » Administration » Configuration »Clientside Validation"
there you can see below picture,

Click on "General settings" and give same setting which is shown below.



After You have to change the "default setting", give same as below or you give as you wish.






Error message placement

Default location

Here you can select the default location to display your error messages. You can choose between the following options:
  • jQuery selector: This option will place a div containing all the error messages inside the div that matches the jQuery selector entered in the "jQuery selector" field below this option.
  • Top of form: This option will place a div containing all the error messages above the form the error messages are for.
  • Before label: This option will place the error before the label of the matching element
  • After label: This option will place the error after the label of the matching element
  • Before input: This option will place the error before the matching element
  • After input: This option will place the error after the matching element
  • Top of first form: This option will place a div containing all the error messages above the first form. This means that when there are multiple forms on a page, the errors of the second form will be added to the error div of the first form and will be displayed above the first form
  • Custom function: (Advanced) Selecting this option requires you to enter a javascript function name in the textfield "Custom function name" below this option. This function name will be given to the errorPlacement option of jQuery.validate()
  •  


Error placement exceptions

For each of the previously mentioned options you can enter form ids (one per line) in the textareas of the "Error placement exceptions" fieldset. Errors for the form matching this form id will appear according to the textarea you entered its form id in. For example if you enter page_node_form in the "Before label" textarea of the "Error placement exceptions" fieldset, all error messages for this form will be displayed before the label of the field they are for, regardless of what the default setting is.


After set all configuration you can check on any form, client side validation will work.





If you are using Drupal 6 then you have install some other sub module for clientside validation which is listed below 
download all submodule then install it.
Drupal 6 submodule for client side validation

clientside_validation
context
elements
fapi_validation
features
field_permissions-7.x-1.0-
beta2
field_validation
ife
testswarm
webform
webform_validation


Enjoy guys, if any issue you can comment here.

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.

Wednesday 17 July 2013

PHP Interview Questions

Q) How the form data is transmitted?

GET:

1) The user agent takes the value of action, appends a ? to it, then appends the form data set, encoded using the application/x-www-form-urlencoded content type. The user agent then traverses the link to this URI. The GET method sends all the gathered information along as part of the URL.
 
2) In this scenario, form data are restricted to ASCII codes means  only ASCII characters allowed..

3) GET is less secure compared to POST because data sent is part of the URL. So it's saved in browser history and 
server logs in plain-text

4)
form data is in the URL and URL length is restricted. A safe URL length limit is often 2048 characters but varies by browser and web server.

5)
7607 character maximum size is allowed.


POST:


1) The user agent conducts an HTTP post transaction using the value of the action attribute and a message created according to the content type specified by the enctype attribute and it to define url.

2) No restrictions. Binary data is also allowed.
3) The POST method transmits the information invisibly to the user.
4) The size of data has no restrictions.
5) 8 Mb max size for the POST method.


Q) Who is the father of HTML?

Tim Berners-Lee  is a British computer scientist, best known as the inventor of the World Wide Web. He is inventor of HTML.

Q) Who is the father of PHP?

Rasmus Lerdorf is known as the father of PHP.PHP/FI 2.0 is an early and no longer supported version of PHP. PHP 3

Q) In how many ways we can retrieve the data in the result set of MySQL using PHP?


  • You can do it by 4 Ways


  • 1. mysql_fetch_row.


  • 2. mysql_fetch_array
    3. mysql_fetch_object
    4. mysql_fetch_assoc


    Q) How can we extract string ‘abc.com ‘ from a string ‘http://info@abc.com using regular expression of PHP?

    We can extract by

    $found="abc.com";
    preg_match("/^http:\/\/.+@(.+)$/","http://info@abc.com&#8217",$found);
    echo $found[1];

    Q) How to check PHP version ?

    echo phpversion();

    Q) What are the current versions of apache, PHP, and MySQL?

    As of February, 2007 the current versions are
    PHP: p
    hp5.5.1
    MySQL: MySQL 5.5
    Apache: Apache 2.2.4

    Q) What are the different types of errors in PHP?

    1. Notices:
    2. Warnings
    3. Fatal errors

    Q) How to read write and delete file in PHP?

    $m_img = "toltrip.png";
    
    $m_img_path = "/var/www/java/jquery/".$m_img;
    echo file_exists($m_img_path);
    if (file_exists($m_img_path))
    {
         unlink($m_img_path);
    }
    // See if it exists again to be sure it was removed
    if (file_exists($m_img_path))
    {
              echo "Problem deleting " . $m_img_path;
    }
    else
    {
            echo "Successfully deleted " . $m_img_path;
    }
    //For write content in file
    
    $fh = fopen('test.html', 'a');
    fwrite($fh, 'Hello world!');
    fclose($fh)
    


    Q) How to split word in to character in PHP?

     We can split word to seprate in  character 
    $pieces = str_split("piece1");
    print_r($pieces);
    

    Q) What is the value of $b in the following code?

        $a="5 USD";
        $b=10+$a;
        echo $b;



    If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.
    The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

    Q) What are super global arrays?

    All variables that come into PHP arrive inside one of several special arrays known collectively as the superglobals. They're called superglobal because they are available everywhere in your script, even inside classes and functions.

    $GLOBALS
    $_GET
    $_POST
    $_SESSION
    $_COOKIE
    $_REQUEST
    $_ENV
    $_SERVER



    Guys I will upload more question later.

    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.


    Sunday 14 July 2013

    Create Drupal 7 custom module

    For custom module in drupal 7, we have create some file which is listed below.


    Create your first "demo" Drupal 7 module with the following steps.
    1. Create a folder called demo in sites/all/modules/custom
    2. Create a demo.info file
    3. Create a template file page-demo.tpl.php in your theme directory
    4. Enable your module at http://domain.com/admin/build/modules
    5. Visit http://domain.com/demo
    This belongs into your demo.info file:
    ;$Id$
    
    name = demo
    description = A demo module created by Ashish.
    package = Drupal 7 Development
    core = 7.x
    files[] = demo.module
    
    ;dependencies[] = autoload
    ;php = 5.2
    
    

    The demo.module file


    /**
    * @file
    * demo - Rick's demo module
    * By Rick Smith of Cryptosmith
    * This thing is too trivial to deserve protection
    * through "intellectual property rights."
    * This is my first attempt to write Drupal code
    * and I probably don't have the format quite right.
    **/
    /* the menu hook - adds the path "/demo" to the site. */

    function demo_menu(){
    $items['demo'] = array(
    'page callback' => 'demo_my_page',
    'access arguments' => array('access content'),
    );
    return $items;
    }


    /* this code (noted in the callback above) creates the
    * contents of the "demo" page */

    function demo_my_page(){
    $form = array();
    $form['demo_my_name'] = array(
    '#type' => 'textfield',
    '#title' => t('My Name'),
    '#default_value' => variable_get('demo_my_name', ''),
    '#required' => TRUE,
    );
    $form['demo_about_me'] = array(
    '#type' => 'textarea',
    '#title' => t('About Me'),
    '#default_value' => variable_get('demo_about_me', ''),
    '#required' => TRUE,
    );

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

    $form['#submit'][] = 'system_settings_form_submit';
    return $form;

    }


    The theme template file page-demo.tpl.php

    print $content;


    Note--Please keep php tags in your module file and tpl file.

    Drupal Installation Guide

    Drupal 7 is a latest content management system. The instruction include commands that can be run from the command line. Every stepcontains a link to more detailed installation instructions.

    Please follow below steps.

    System requirements
    Step 1: Download and extract Drupal
    Step 2: Create the database
    Step 3: Create the settings.php file
    Step 4: Run the installation script


    Server Requirements:

    1. Apache (version 2.0 or greater)
    2. PHP 5 (5.2.0 or greater)
    3. MySQL (5.0 or greater) or PostgreSQL (8.3 or greater) or SQLite (3.4.2 or greater)
    1. Download CMS Files:
    Download Drupal 7 from here click here and extract the files with a compression tool (such as tar file)


    tar -zxvf drupal-7.x.tar.gz
    Move the files into a directory within your web server's document root or your public HTML directory.
    On many *nix computers the path from the server's root will be /var/www/
    mv drupal-7.x /var/www/
    2.Create configuration file:

    Create a configuration file by copying the example, named default.settings.php in the sites/default directory. The copied configuration file must be named settings.php
    3.Create the Drupal database:
     You must create a MySQL database for your Drupal site manually.

    4. Run the installation script:

    To run the installation script point your browser to the base URL of your website (e.g.,
    http://www.example.com, http://www.example.com/drupal7 or http://localhost/drupal7).



    Dru


    Note--Now here you have to give permission on project folder, just put below command

    chmod -R 777 /var/www/drupal/

    Now it will work and it will go for further detail like as database name which you have created manually and username,password of database




    Now Your Drupal project is successfully installed.