I have a very basic module, myforms.module :

<?php
// $Id: myforms.module
// something

function myforms_form_alter($form_id, &$form) {
  print_r("This is the form id: " . $form_id);
  if ($form_id == 'some_form') {
    echo "Hi there<br>";
  }

This module does its amazing stuff :-)

But now I would like to add some PHP code to be executed upon submission of some_form . And for one reason or the other, I can't get it to work. How? How? How??? Could someone give me a working example just issuing an "echo" thingy upon submission? One limitation: upon submitting, things are saved (by Drupal, not by my own code). Obviously, that saving should still be done. I just want to issue an extra "echo". How do I do that? And where do I put this extra submit code?

Using Drupal 5.x

Comments

marcvangend’s picture

You can add your own submit handler (a function that runs when a form is submitted) using the form api: http://drupal.org/node/58689.
If the form being submitted is a node form, you can also use hook_nodeapi when $op=presave or $op=insert (see http://api.drupal.org/api/function/hook_nodeapi/6).

modul’s picture

Thanks, MarcVanGend and MTSanford, I "feel" I'm getting there, but not quite...

This is myforms.module:

<?php
// $Id: myforms.module
// something

function myforms_form_alter($form_id, &$form) {
  print_r("This is the form id: " . $form_id); 
  if ($form_id == 'my_special_type_of_node_form') {
    echo "Hi there<br>";
    $form['submit'] = array(
	   '#type' => 'submit',
	   '#value' => 'Save this please',
	   '#submit' => array('my_special_submit_function')
	   ); 
  }
}

And then in a .php file which contains my other readily available goodies, I added:

function my_special_submit_function(($form_id, $form_values) {
   echo "This is the form id in my submit function: " . $form_id . "<br>";	
}

BUT:
- my submit button is no longer there...
- and the echo from my special submit function is (well, obviously) not seen, i.e. I can't get to my function

I admit, Drupal forms are still, after sooooo many months, one big labyrinth to me. But if you guys could help me out of this one...

marcvangend’s picture

Look closely at the documentation on http://drupal.org/node/58689, it seems as if you didn't do exactly what it says. Pay attention to the difference between $form['submit'] and $form['#submit']. Make sure that you add a handler to the $form['#submit'] array instead of completely overriding it.

Again, if this is a node form your editing, hook_nodeapi might be better/easier. Try this for instance:

function myforms_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':
      drupal_set_message("I'm saving this node:<br />" . print_r($node, 1));
  }
}
modul’s picture

You made my day, MarcVan Gend... I finally have it working. Obviously, I messed up by overriding the entire submit thingy. Thanks for your most generous help!!

marcvangend’s picture

You're welcome. Thanks for the feedback, I like making days :-)

mtsanford’s picture

It looks like you're writing *OVER* your submit button, not altering it (which is the point of hook_form_alter)

Probably just something like this

function myforms_form_alter($form_id, &$form) {
  print_r("This is the form id: " . $form_id);
  if ($form_id == 'my_special_type_of_node_form') {
    $form['submit']['#submit']['my_special_submit_function'] = array();
   );
  }
}
mtsanford’s picture

... which would add a submit function for the button,

or probably better:

function myforms_form_alter($form_id, &$form) {
  print_r("This is the form id: " . $form_id);
  if ($form_id == 'my_special_type_of_node_form') {
    $form['#submit']['my_special_submit_function'] = array();
   );
  }
}

Which would add a submit function for the entire form.

modul’s picture

Thanks, MTSanford, your help also meant a great relief for me! I'm starting to see a litttttle clearer now.