* if i will click a input button in basic page a particular function should be called from my custom module. I have already enabled my module in my instance.*

$name = $_POST['name'];
$pwd = $_POST['pwd'];
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<p>Name<br> <input type="text" name="name"><br></p>

<p>Password:<br><input type="password" name="pwd"><br></p>
<input type="submit"  value="Register">

i am using this code...and its working but the problem is while refreshing the page, function automatically gets called without clicking button... I wrote this code in Basic Page...

Please guys tell me the proper way to call the functioin from my basic page.

I just wanted to execute my cutome module function after clicking submit button...is there any simple way to do that...

3 Answers 3


I guess you want to create a menu callback which will call your form right? Here is how to-

Create your menu hook like bellow:

function mymodule_menu($node) {
  $items['my-frm'] = array(
    'type' => MENU_CALLBACK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_form'),
    'access callback' => TRUE,
  return $items;

Than create your form like below:

function my_form() {
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  return $form;

And after that here is your form submit:

function my_form_submit() {
  drupal_set_message("Hello World my form submit hook is called here");  

Do a clear cache and navigate to yoursite/my-frm link you should see your form.

  • 1
    He is not asking for a menu item to trigger the form, but how to execute a custom function when submitting it. As your answer is closer to the solution, you might just extend your example to how to add a form to a Drupal 7 page. The current approach of writing the markup to the page directly is not Drupal way of coding. Commented Jan 30, 2016 at 7:42
  • exactly Mario Steinitz ....i want to call a function when i submit button...do you have any solution or any tutorial from where i can get proper idea about this.... Commented Jan 30, 2016 at 9:44

I think you are taking all the wrong steps.

In drupal you do this by implementing hook_form_alter in your custom module and do something like the following:

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'YOUR-FORM-ID') { // identify your form
        $form['#submit'][] = 'YOUR_CUSTOM_FORM_SUBMIT_HANDLER'; // set your form-submit handler

function YOUR_CUSTOM_FORM_SUBMIT_HANDLER(&$form, &$form_state){

The YOUR_CUSTOM_FORM_SUBMIT_HANDLER is where you write code to process the submitted data however you like.

However, the form that was submitted must be one generated by Drupal's form-api.

  • I think he is trying to define a new form.
    – user18099
    Commented Jan 30, 2016 at 8:33
  • Thanks for giving answer...but i want to call a function which is in my custom module when i click submit button...that function can return anything like single echo statement.. Commented Jan 30, 2016 at 9:41
  • @MukeshGami: I know, make $form['#submit'][] point at your custom module function. As for returning a value, maybe set the value in the drupal variable table. Then you can call variable_get anywhere else to retrieve it
    – sisko
    Commented Jan 30, 2016 at 11:47

You are trying to add a form to a page. But if you just add PHP code to your page, then this code will be executed every time you call the page.

Luckily, form building is easy in Drupal. You define one function to layout your form (hook_form), and another function to handle the results (hook_submit).

Have a look at the https://www.drupal.org/project/examples "Code examples for developers". You can copy&paste form building examples from there. It will also help you understand where to put code in other cases.

Specifically, do not add your homefun() function to the page itself. Put that into the hook_submit() function.

  • thank you...but where should i implement hook_submit() function Commented Jan 30, 2016 at 9:46
  • Make a simple module. (just add minimal module files) Your own code goes into your own modules. (always) Your _submit and _form functions go into your new module. Here is a link to an easy tutorial (befused.com/drupal/first-module)
    – user18099
    Commented Jan 31, 2016 at 9:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.