0

I'm building a custom module and I would like to add some buttons that redirect the user to other .inc files on click.

My idea was something like this, in my mymod.user.inc file :

if ($form_state['storage']['step'] === 1) {

 $form['button']['addnew_form'] = array(
    '#type' => 'submit',
    '#value' => 'new form',
    '#submit' => array('mymod_form_user_newform_form'),
            );

$form['button']['addnew_form2'] = array(
    '#type' => 'submit',
    '#value' => 'new form 2',
    '#submit' => array('mymod_form_user_newformtwo_form'),
            );

 return $form;    

}

I added a hook_menu in my myform.module file :

function myform_menu(){
$items = array();

$items['myform/forms/newform'] = array(
    'title' => 'New form 1',
    'description' => 'New form 1',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('mymod_form_user_newform_form'),
    'file' => 'myform.newform.inc',
    'access callback' => TRUE
);

When I click on my button, it doesn't redirect to the new form page. Am I totaly wrong using this method ?

1
  • You should redirect form to another url rather than PHP include file. You also need to share the submit function of the your form. Jul 17, 2013 at 8:39

1 Answer 1

3

You redirect to a URL, you can't redirect to a PHP include file it's not possible.

The function you give as the #submit key is run on form submission, it's not used to build up the next page request or anything like that.

Your code should look more like this:

$form['button']['addnew_form'] = array(
  '#type' => 'submit',
  '#value' => 'new form',
  '#submit' => array('MYMODULE_form_submit'),
);

Then you'll need a submit handler to match that:

function MYMODULE_form_submit($form, &$form_state) {
  // Redirect to the appropriate URL.
  $form_state['redirect'] = 'myform/forms/newform';
}

You can add a second submit handler for your second button, with a different function name, and that redirects to a new menu item that you need to define which contains the second form.

1
  • That's it, thanks ! I came to the same logic while reading.
    – user18713
    Jul 17, 2013 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.