Here's my problem.
Basically, I have form, on submission the form calls it's _submit function (mytest_form_submit), basic Forms API stuff. The _submit function returns a contructed URL to another callback in the system.
I'm using a block with a form on it, and just putting the block in one of the columns.

Now, Using the form from any page except the forms' results page works fine. After submission you are redirected with the return value from mytest_form_submit, to a results page.

Submissions not from the results page have the following log entries:

[] [error] [client *.*.*.*] MYTEST_FORM_SUBMIT####, referer:http://***/tests/results/0/1/2/test
[] [error] [client *.*.*.*] RESULTS ACTION###, referer: http://***/tests/results/0/1/2/test

Any form submissions from this results page ends up running the callback twice. I guess once for submitting to itself and then again with the returned url from the _submit function.

Here's the error log output for a submission from the results page.

[] [error] [client *.*.*.*] RESULTS ACTION###, referer: http://***/tests/results/0/1/2/test
[] [error] [client *.*.*.*] MYTEST_FORM_SUBMIT####, referer:http://***/tests/results/0/1/2/test
[] [error] [client *.*.*.*] RESULTS ACTION###, referer: http://***/tests/results/0/1/2/test

I guess my first question is Am I doing this right?
I tried setting a "has_run" variable on $_POST and using the '#action' attribute on the form, to get the callback to be ignored, but nothing worked.

The code below is an outline so it is easier to understand, and it does run.

function form_submit_tester_menu($may_cache) {
    $items = array();

    if($may_cache) {
        $items[] = array(
            'path' => 'tests',
            'title' => 'Tests',
            'callback' => 'test_page',
            'type' => MENU_CALLBACK,
            'access' => TRUE,
        );
        $items[] = array(
            'path' => 'tests/results',
            'title' => 'Test Results',
            'callback' => '_mytest_results',
            'type' => MENU_CALLBACK,
            'access' => TRUE,
        );
    }
    return $items;
}

function form_submit_tester_block($op = 'list', $delta = 0) {
    // listing of blocks, such as on the admin/block page
    if ($op == "list") {
        $block[0]["info"] = t('Form Submit Test Block');
        return $block;
    }
    else if ($op == 'view') {
        $block['content'] = drupal_get_form("mytest_form");
        return $block;
    }
}                                                                                                                 
                                                                                                                  
function test_page() {                                                                                            
    return drupal_get_form('mytest_form');                                                                        
}                                                                                                                 
                                                                                                                  
function mytest_form() {                                                                                          
    $form['mytest_field'] = array(                                                                                
        '#type' => 'textfield',                                                                                   
        '#title' => 'TEST FIELD',                                                                                 
        '#size' => 15,                                                                                            
    );                                                                                                            
    $form['mytest_submit'] = array(                                                                               
        '#type' => 'submit',                                                                                      
        '#value' => 'Submit'                                                                                      
    );                                                                                                            
    return $form;                                                                                                 
}                                                                                                                 
                                                                                                                  
function mytest_form_submit($form_id, $form_values) {                                                            
    error_log("MYTEST_FORM_SUBMIT####");                                                                          
    return "tests/results/0/1/2/".$form_values['mytest_field'];                                                   
}                                                                                                                 
                                                                                                                  
function _mytest_results($a, $b, $c, $d) {                                                                        
    error_log("RESULTS ACTION###");                                                                               
    return "<div>*<br/>$a<br/>$b<br/>$c<br/>$d<br/>*</div>";                                                      
}         

Thanks in Advance
Eric.

Comments

Gman’s picture

A quick non-FAPI hack would to place a static variable in your function which will check if it is the first time through the function.

static count = 0;

if (!count) {
.... execute code

}
count++;

The second time through this code, count will equal 1, so the if will not fire.
I am awaiting the correct answer, but hopefully this will let you progress.

----------
Blog
How To Do Things.com

emackn’s picture

I'm surprised that there are not more responses. ??

I have been looking at the search module, and node_search hook for hints and one thing I found is that somehow the $op variable for a search changes during submission process. It does three $op == "name" calls then a $op == "search" which runs the new search..

What am I missing?

panis’s picture

Forms handling in drupal however advanced it may be - is a pain to use in situations where a form is not used to just submit data to save or update a database.

Here is the work around I used to avoid the situation you are facing.

<?
function hook_menu()
{
 /* define a path to a "blank" page. */
$items[] => array( 'path'=>'blank', 'callback'=>'_blank_page', 'access'=>TRUE,'type=>MENU_CALLBACK);
..
}

/* declare the blank page callback. */
function _blank_page()
{
return '';
}

function my_form()
{/* set the action handler to blank page. Irrespective of which page the form is submitted from it will
 * always use this action handler.
 */
$form['#action']='?q=blank';
...
}

This should fix your problem. It did mine. Have absolutely no idea what side-effects this might create but havent noticed any yet on my site.

emackn’s picture

Very Nice.
I'll have to go back and revisit my code and try that out. I like this much better than my non-submit button and javascript solution.

...
    $form['mytest_button_submit'] = array(
        '#type' => 'button',
        '#value' => 'Submit',
        '#attributes' => array('onclick' => 'this.submit(); return true;'),
    );
...

Berto’s picture

I got this going just now, thank you very much! Great hack solution :)

eric-sf’s picture

I ran into the same problem this code should fix it.

$form['#redirect'] = FALSE;

Berto’s picture

editing original post

I thought this worked for a second, but it doesn't. I'm going to try the solutions above

himerus’s picture

This method worked for me while developing an advanced user profile search. It was calling the callback twice, which would skew the conditionals in the function. Setting #redirect to false fixed this, and now I'm getting proper functionality for a browse feature on first loading the page, and search results on a submit of the included form.

Perfect, and thanks @ericxq!

Jake Strawn
Personal Blog - http://himerus.com
Himerus Inc. - http://himerusinc.com
Online Resume - http://jakestrawn.com