Hi,

I'm trying to build a multi-page form but nothing happens when submit is clicked after the first step.
This is (part of) my code for creating the form.

<?php
function lesplanning_add_form($form_state = NULL) {
   
//first check user role
   
global $user;
   
// Check to see if $user has the docent role.
   
if (in_array('docent', array_values($user->roles))) {
      

    }else {
       
//do leerling stuff
       
if(!isset($form_state)){
           
$step = 1;
        }else{
           
$step = $form_state['step'] +1;
        }

       
$form['step'] = array(
           
'#type' => 'hidden',
           
'#value' => $step,
        );

        switch(
$step){
           
//start of by selecting subject
           
case 1:
               
//get subjects from db
               
$result = db_query("SELECT vak FROM {subjects}");
               
$options = array();
                while(
$row = db_fetch_array($result)){
                   
$options[] = $row['vak'];
                }
               
$form['subject'] = array(
                   
'#type' => 'checkboxes',
                   
'#title' => 'Selecteer het vak waar je bijles voor wil',
                   
'#options' => $options
               
);

               
$form['choice'] = array(
                   
'#type' => 'radios',
                   
'#title' => 'Wil je een datum kiezen of een docent voor je bijles?',
                   
'#options' => array('docent', 'datum')
                );

                break;

            case
2:
               
//choose docent or date?
               
if($form_state['values']['choice'] == 'docent'){
                   
//get docent for the subject from db
                   
$result = db_query("SELECT u.name, u.id
                        FROM {users} as u, {profile_fields} as pf, {profile_values} as pv
                        WHERE pv.uid = u.uid AND pv.fid = pf.fid AND pf.title = 'Vakken' AND pv.value = '%s'"
,
                        array(
'pv.value' => $form_state['values']['subject']));

                   
$docenten = array();
                    while(
$row = db_fetch_array($result)){
                       
$docenten[] = $row['name'];
                    }

                   
$form['subject'] = array(
                       
'#type' => 'hidden',
                       
'#value' => $form_state['values']['subject'],

                    );

                   
$form['choice'] = array(
                       
'#type' => 'hidden',
                       
'$value' => $form_state['values']['choice']
                    );

                   
$form['docent'] = array(
                       
'#type' => 'radios',
                       
'#title' => 'Kies hieronder de docent',
                       
'#options' => $docenten
                   
);

                }

                if(
$form_state['values']['choice'] == 'datum'){

                }
               
                break;          
        }

       
$form['#multistep'] = TRUE;
       
$form['#redirect'] = FALSE;

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

        return
$form;
    }

    return
$form;
}
?>

I'm clueless on what the problem might be so any tips would be greatly appreciated.
Thanks in advance, Bouke

Comments

I really seem to be stuck on

I really seem to be stuck on this problem... I've done some more research on the multipage forms.
The problem I now discovered is that in Drupal 6 one needs to use form_state['rebuild'] = TRUE; in the submit function. Yet I still don't seem to make any progress because no matter what I do I just won't get to the next step in my form. Please help because I will never be able to finish my project without this form.

This is my current code:

<?php
/*
* form to add appointments or give availability
*/
function lesplanning_add_form($form_state) {

        
#print_r($form_state);
        //do leerling stuff
       
if(!isset($form_state)){
           
$step = 1;
        }else{
           
$step = $form_state['storage']['values']['step'] + 1;
        }

       
$form['values']['step'] = array(
           
'#type' => 'hidden',
           
'#value' => $step,
        );

        switch(
$step){
           
//start of by selecting subject
           
case 1:
               
//get subjects from db
               
$result = db_query("SELECT vak FROM {subjects}");
               
$options = array();
                while(
$row = db_fetch_array($result)){
                   
$options[] = $row['vak'];
                }
               
$form['subject'] = array(
                   
'#type' => 'radios',
                   
'#title' => 'Selecteer het vak waar je bijles voor wil',
                   
'#options' => $options
               
);

               
$form['choice'] = array(
                   
'#type' => 'radios',
                   
'#title' => 'Wil je een datum kiezen of een docent voor je bijles?',
                   
'#options' => array('docent', 'datum')
                );

                break;

            case
2:
              
//build step 2     
               
break;

            case
3:
               
//build step 3
               
break;
        }       

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

/*
* function to submit lesplannning_add_form
*/
function lesplanning_add_form_submit($form, $form_state) {
           if (empty(
$form_state['storage']['values'])) {
             
// if there is no previous values redraw for second step
             
$form_state['storage']['values'] = $form_state['values'];
             
$form_state['rebuild'] = TRUE;
         }else {
             
//parse form
        
}   
}
?>

if(!isset($form_state)){  

if(!isset($form_state)){
   $step = 1;
}else{
   $step = $form_state['step'] +1;
}

$form_state is always set. That means $step will never equal anything other than 1.

Full-time freelancer, always looking for work.
jaypan.com (my portfolio)

I changed it

I changed it to

<?php
if(!isset($form_state['storage']['values']['step'])){
           
$step = 1;
        }else{
           
$step = $form_state['storage']['values']['step'] + 1;
        }
?>

But unfortunately that doesn't seem to work either. Am I missing sth here or just being pain stupid ? :p
I've been on this for hours now and just can't solve it...

$form_state['storage']['value

$form_state['storage']['values']['step'] is never set and $step will always be 1.

Full-time freelancer, always looking for work.
jaypan.com (my portfolio)

I now tried changing my

I now tried changing my submit function a bit.

it now looks like

<?php
if (empty($form_state['storage']['values'])) {
             
// if there is no previous values redraw for second step
             
$form_state['step'] = $form_state['values']['step'];
             
$form_state['rebuild'] = TRUE;             
         }else {
             
//parse form
        
}
?>

I also changed the step check to:

<?php

if(!isset($form_state['step'])){
           
$step = 1;
}else{
           
$step = $form_state['storage']['values']['step'] + 1;
        }
?>

This way form_state['step'] should be passed to the form right? Causing the check to return true in isset($form_state['step']) and after that the adding 1 to the step
Yet it still doesn't work. Any other tips?

After printing the form_state

After printing the form_state array I can see why form_state['step'] isn't working. Yet I don' t really know how to acces it cleanly. Any ideas on that?

Array ( [storage] => [submitted] => [post] => Array ( [step] => 1 [subject] => 0 [choice] => 0 [op] => Submit [form_build_id] => form-e5a6f6abf268f77f517816c9b24fdcbc [form_token] => 8a2ccc04e8d2bc0061d467bb6b472056 [form_id] => lesplanning_add_form ) )

I'm going to give you an

I'm going to give you an example of a multi-step form I am building right now. You can take that and try to work your own form into a similar logic. I'm not going to write your code, but I'll help point you in the right direction. This form has a first page, an unknown number of middle pages, and a last page. I'm only showing you the logic here - I'm removing most of the code as it's not relevant to what I'm trying to show.

<?php
function slideshow_framework_image_import($form_state)
{
    if(!isset(
$form_state['storage']))
    {
       
// Output first page
   
}
    elseif(isset(
$form_state['storage']['check_images']) && $form_state['storage']['check_images'])
    {
       
// Output middle pages
   
}
    else
    {
       
// Output last page
   
}
    return
$form;
}

function
slideshow_framework_image_import_submit($form, &$form_state)
{
   
// Process first page
   
if(!isset($form_state['storage']))
    {
       
$form_state['storage']['check_images'] = TRUE;
       
$form_state['storage']['images_to_check'] = array('something', 'something else', 'more other stuff'); // this actual data is irrelevant to the tutorial.
   
}
   
// process middle pages
   
elseif(isset($form_state['storage']['check_images']) && $form_state['storage']['check_images'])
    {
       
array_shift($form_state['storage']['images_to_check']);
        if(!
count($form_state['storage']['images_to_check']))
        {
            unset(
$form_state['storage']['check_images']);
        }
    }
   
// process last page
   
else
    {
       
// Here I process all the submitted data, do any database queries etc.
   
}
}
?>

I've removed most of the form and the submit function, I've only left in the bits that are relevant to the multi-page logic. Go through it and see how it enters each of the three sections. Then see if you can work your own module in the same type of manner. Of course it won't be exactly the same, but seeing the logic behind how it can be done should help you.

Full-time freelancer, always looking for work.
jaypan.com (my portfolio)

I understand you don't want

I understand you don't want to write my code. I don't mean to have you do that at all. However, I've been looking at pieces of code like this for two days now and I still can't figure it out.

Just to see if I'm plain stupid or sth else might be wrong I pasted your code (only very slightly modified) into my module and, no surprise here, it worked. So step by step I tried modifying it further and the minute I changed $form_state['storage]['images_to_check'] from an array to a variable it broke down again. I think I can figure it out now so thanks a lot for your time and help.

I was just wondering if you have any idea why you can't seem to pass a variable but only arrays?

That's because of this line

That's because of this line of code:

<?php
if(!count($form_state['storage']['images_to_check']))
{
    unset(
$form_state['storage']['check_images']);
}
?>

It's counting the number of elements in the array. When there are no more elements, it unsets $form_state['storage']['check_images']. When this element is not present, the last step of the form is entered. The problem is that if this is a variable and not an array, then count() function is not going to work, since there are no array elements to count. You will have to adjust this accordingly to work with your variable.

I can't give you any specifics on this however without knowing what your code looks like now. You can post it up if you want, but it's almost 2AM here and I'm going to bed. Maybe someone else can help you from there, or I can look at it tomorrow at some time.

Full-time freelancer, always looking for work.
jaypan.com (my portfolio)

I may be wrong here, but,

I may be wrong here, but, from what I can see, you're setting $step, but failing to set your $form_state['step'] value. That is to say:

<?php
if(!isset($form_state['step'])){
           
$step = 1;
}else{
           
$step = $form_state['storage']['values']['step'] + 1;
        }
?>

This tells me that $step is either 1 or some value stored in $form_state['storage']['values']['step'] + 1.

<?php
              $form_state
['step'] = $form_state['values']['step'];
?>

This sets your ['step'] box to whatever is in your ['step'] box. But you haven't set that box to $step yet.

So, you iterate, $step gets set to 1, then you iterate again, but because you haven't stored $step in the $form_state, it's still a null value. So it reiterates to 1 again. And does that infinitely.

nobody click here