In a nutshell, your form is effectively submitted twice during the process of the Form API doing it's thing. The first time your redirect is correct, but the second time the referer is actually the node submission form, and this over-writes your original, intended redirect path.

Let's think about this. We can't do this while we build or alter our form, but one of the last things that happens in the Form API process is the form's submit function, so why can't we hijack that in our module? Well, we could. But we need the function called node_form_submit to fire on submit in order to create our content and it contains the redirect to the newly created node, which we'll need to somehow override, since we're not using the Form API any more. To do this you effectively have to keep a re-written copy of at least one core function in your own module, which sucks! You've just created code you have to maintain to keep step with Drupal updates. =(

But there is an approach which will allow you to achieve your redirect to the referring page, without duplicating any core functions or creating anything nasty which you have to maintain. I documented it here:http://drupal.org/node/134000#comment-799369

Basically, you store the referring URL (either in the session or in a custom CCK field in your content type) and use the 'insert' operation of hook_nodeapi to handle your message setting and redirection. Perfect!

Note, this is not necessary in Drupal 6.x - there is a new form history part of the API allowing you to pick out the prior redirect and use it. Much neater! =)

We’re Code Enigma

We’re one of the most experienced Drupal teams in Europe, best known for our work on large, technically challenging projects for all kinds of clients.

Our team is passionate about Drupal and open source software. Our whole company spends at least four weeks per year working on Drupal modules or other open source projects. We’re also strongly committed to putting design first, taking a mobile-first, content-out approach to creating websites. This ensures that the sites we build combine the power of Drupal with best practice design and development.