Advertisement
Guest User

Untitled

a guest
Oct 19th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. //function registration_install(){}
  4. //function registration_uninstall(){}
  5.  
  6. //diplay help content
  7. function registration_help($path) {
  8.   $output = '';  //declare your output variable
  9.   switch ($path) {
  10.     case "admin/help#registration":
  11.       $output = '<p>'.  t("Get the user data with a simple registration form .") .'</p>';
  12.       break;
  13.   }
  14.   return $output;
  15. } // function registration_help
  16.  
  17.  
  18.  
  19. // Valid permissions for this module
  20. function registration_perm() {
  21.   return array('access registration content', 'create registration', 'administer registration');
  22. } // function registration_perm()
  23.  
  24. /**
  25.  * drupal_get_form() callback. No need to pass $form and $form_state as they are not used.
  26.  */
  27. function registration_register_form(){
  28.     $form = array();
  29.  
  30.     $form[name] = array(
  31.     '#type' => 'textfield',
  32.     '#title' => t('Enter your name'),
  33.     '#maxlength' => 30,
  34.     '#required' => TRUE,
  35.     '#size' => 20,
  36.     );
  37.    
  38.     $form[family] = array(
  39.     '#type' => 'textfield',
  40.     '#title' => t('Enter your family name:'),
  41.     '#maxlength' =>50,
  42.     '#required' => TRUE,
  43.     '#size' => 20,
  44.     );
  45.  
  46.     $form[id] = array(
  47.     '#type' => 'textfield',
  48.     '#title' => t('Enter your ID number:'),
  49.     '#maxlength' => 8,
  50.     '#required' => TRUE,
  51.     '#size' => 8,
  52.     '#element_validate' => array('numric_validate'),
  53.     '#ahah' => array(
  54.           'event' => 'focusout',
  55.           'path' => 'registration/js_idcheck',
  56.           'wrapper' => 'div_idcheck',
  57.           'method' => 'replace',
  58.           'effect' => 'fade'),
  59.     );
  60.    
  61.     $form['div_idcheck'] = array(
  62.     '#type' => 'value',
  63.     '#value' => '<div id="div_idcheck"></div>',
  64.     );
  65.      
  66.     $form['submit'] = array(
  67.     '#type' => 'submit',
  68.     '#value' => t('send'),
  69.     );
  70. return $form;
  71. }//registration_form
  72.  
  73. /**
  74.  * submit handler for registration_register_form().
  75.  * no need to pass $form_state by reference.
  76.  * corrected some sql injection stuff.
  77.  * removed validation stuff for #required fields as they are required above.
  78.  */
  79. function registration_register_form_submit($form, $form_state){
  80.     $id = $form_state['values']['id'];
  81.     $name_f = $form_state['values']['name'];
  82.     $family = $form_state['values']['family'];
  83.    
  84.     // not necessary tovalidate #required = TRUE fields/
  85.     $sql='SELECT * FROM {registration} WHERE id = %d';
  86.     $result=db_query($sql, $id);
  87.     if(db_num_rows($result) != 0){
  88.         form_set_error('id' , 'Your entered id is already exist');
  89.         return;
  90.         }
  91.        
  92.     db_query("INSERT INTO {registration} (name, family, id) VALUES ('%s', %s, %d)", $name_f,$family,$id);
  93.     if (db_error === FALSE) // what ?
  94.         drupal_set_message(t('Your form has been saved.'));
  95.     else
  96.         drupal_set_message(t('some error happen.please retry again.'), 'error');
  97. }
  98.  
  99.  
  100. function registration_admin_settings() {
  101.  
  102.  return 0;  
  103. }//registration_admin_settings
  104.  
  105. function registration_menu() {
  106.  
  107.   $items = array();
  108.  
  109.   /*$items['admin/settings/registration'] = array(
  110.     'title' => 'View the successful registration.',
  111.     'description' => 'Show the list of registred forms',
  112.     'page callback' => 'drupal_get_form',
  113.     'page arguments' => array('registration_admin_setting'),
  114.     'access arguments' => array('administer registration settings'),
  115.     'type' => MENU_NORMAL_ITEM,
  116.    );
  117.    * */
  118.  
  119.   //link for registration form
  120.   $items['registration'] = array(
  121.     'title' => 'Registration form',
  122.     'page callback' => 'drupal_get_form',
  123.     'page arguments' => array('registration_register_form'),
  124.     'access arguments' => array('access registration content'),
  125.     'type' => MENU_NORMAL_ITEM,
  126.   );
  127.  
  128.   //link for submit validate script
  129. /*   $items['registration/js_submit'] = array(
  130.     'page callback' => 'js_submit',
  131.     'access arguments' => array('access mymodule js'),
  132.     'type' => MENU_CALLBACK,
  133.   );*/
  134.  
  135.   //link for user id validate script
  136.    $items['registration/js_idcheck'] = array(
  137.     'page callback' => 'js_idcheck',
  138.     'access arguments' => array('access mymodule js'),
  139.     'type' => MENU_CALLBACK,
  140.   );
  141.  
  142.   return $items;
  143. }//registration_menu
  144.  
  145. //check id field for duplicate entery in database
  146. function js_idcheck(){
  147.     $id=$form_state[values][id];
  148.     $sql="select * from {registration} where id = $id";
  149.    
  150.     $result = db_query($sql);
  151.     if(db_num_rows($result) != 0)
  152.         return drupal_json(array('status' => FALSE, 'data' => "This ID is already existing in database."));
  153.     else
  154.         return drupal_json(array('status' => TRUE, 'data' => "This ID is new."));
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement