hi!

I'm working on a multistep(2) form.
On the second page I make a random number to use as an reference number.
After submit the user redirected to a new "thanks" page where this reference number displayed. this is the code:

do {
$kerdoivrefid = mt_rand(100000,999999);
$query = "SELECT refid FROM {kerdoiv} WHERE refid='".$kerdoivrefid."'";
$result = db_query($query);
$rows = db_num_rows($result);
} while ( $rows!=0 );

$form['kerdoiv']['refidvalue'] = array(
'#value' => $kerdoivrefid
);

$form['kerdoiv']['refid'] = array(
'#type' => 'value',
'#value' => $kerdoivrefid
);

In the submit function I put the value of $form_values['refid'] to a session variable.
$_SESSION['user_refid'] = $form_values['refid'];
On the thanks page I display this session variable.
My problem is that the number displayed on the second step of the form and the number displayed on the thanks page are not equal.
It seems that after I display the $kerdoivrefid variable somehow I get a new random number.
First I used a texfield to display $kerdoivrefid:

$form['kerdoiv']['refid'] = array(
'#type' => 'textfield',
'#value' => $kerdoivrefid
);

It worked fine. The displayed value and the value of the session variable was equal. Of course it was not good for me because the user could change this reference number in the textfield.
In the submit function the modul send an message to the user email address whit this reference number. The number in the message and the number in the session variable are equal.

I made a sample here:
http://www.mhn.hu/drupal/?q=kerdoiv

thank for help!

Comments

mooffie’s picture

You need to store this magic number somewhere for it to be remembered.

FormAPI, of Drupal 5, doesn't store interim data for us --that's why we ourselves have to use hidden fields (or sessions, or caches) to store data from previous steps.

But since you can't store this number in a hidden field or in a textfield, for security reasons, then store it in the session. You don't need to put it on the form; you don't even need to use #type=>value.

(#type=>value didn't work for you becuase 'value' types are stored nowhere. They are generated anew whenever encountered. Usually there's no problem with this, because the computation gives us the same result, but in your case the computation isn't 'deterministic'.)

do {
$kerdoivrefid = mt_rand(100000,999999);
$query = "SELECT refid FROM {kerdoiv} WHERE refid='".$kerdoivrefid."'";
$result = db_query($query);
$rows = db_num_rows($result);
} while ( $rows!=0 );

Just do $kerdoivrefid = db_result(db_query_range("SELECT refid FROM {kerdiov} ORDER BY rand()",0,1));.

criznach’s picture

Here's an example from a multistep I did... Step 1 asked if there was a previous registration number, If not, step 2 assigned a new one that is generated from other information.

In step 2 of the form builder...

      // Now Step 2
      $form['registration'] = array(
        '#type' => 'textfield',
        '#title' => t('Suggested Registration'),
        '#description' => $form_values['is_registered'] ? t('Suggested registration number based on step 1.  Change if necessary.') : t('Horse is not registered.  Specify outside registry # here.'),
        '#required' => $form_values['is_registered'] ? TRUE : FALSE,
        '#default_value' => $form_values['is_registered'] ? $_SESSION['last_generated_reg'] : '',
        '#weight' => $wt++,
      );

In the submit handler...

	if ($form_values['step'] == 1) {
		$_SESSION['last_generated_reg'] = registry_horse_generate_registration($form_values);
	}