Saturday, January 29, 2011

Simple Form in Drupal

  1. function transport_services_perm() // used for setting permission
  2. {
  3. return array('access content transport_services');
  4. }

  5. function transport_services_menu() // creating menu
  6. {
  7. $items['transport_services'] = array(
  8. 'page callback' => 'transport_services_page',
  9. 'access arguments' => array('access content transport_services'),
  10. 'access callback' =>'user_access',
  11. ); return $items;
  12. }

  13. function transport_services_page()
  14. {
  15. $output='Welcome to R';
  16. $output .= drupal_get_form('transport_services_form1');
  17. return $output;
  18. }

  19. // Actual form being defined here....

  20. function transport_services_form2($form_id, $form_state = NULL)
  21. {
  22. $form['reg_no'] = array(
  23. '#title' => t('Registration Number:'),
  24. '#type' => 'textfield',
  25. '#size' => '25',
  26. '#required' => TRUE,
  27. '#prefix' => '
    ',
  28. '#suffix' => '',
  29. );
  30. $form['reg_name'] = array(
  31. '#title' => t('Registration Name:'),
  32. '#type' => 'textfield',
  33. '#size' => '25',
  34. '#required' => TRUE,
  35. '#prefix' => '
    ',
  36. '#suffix' => '',
  37. );
  38. $form['submit'] = array(
  39. '#type' => 'button',
  40. '#value' => t('Submit'),
  41. '#validate' => array('transport_services_renewal_output')
  42. );
  43. return $form;
  44. }
  45. // Submit Function is defined....

  46. function reusable_services_form1_submit($form_id,$form_state)
  47. {
  48. $reg_no = $form_state['values']['reg_no'];
  49. $reg_name = $form_state['values']['reg_name'];
  50. db_query("INSERT INTO {registration} (registration_no) VALUES('%d','%s')",$reg_no,$reg_name);
  51. drupal_set_message(t(“No==>”.$reg_no));
  52. drupal_set_message(t(“Name==>”.$reg_name));
  53. drupal_set_message(t(“Successfully inserted values”));
  54. }

1 comment:

  1. Nice details there... its very usefull for drupal programmers even me also ....

    ReplyDelete