1

I am newbie in Drupal, I have a query that i want to pass some value from form creation function to its submit handler.

Right now, I am saving that value in a form element of hidden type.

$form['product_nid'] = array(
 '#type' => 'hidden',
 '#value' => 12,
)

But this is not safe, anyone can change value on client side.

1
  • Basically you are asking for the most secure way, are you?
    – leymannx
    Aug 28, 2015 at 7:43

2 Answers 2

2

It is easy to do by using $form_state['storage'],

Just define $form_state['storage']['something'] = ; This value will not show in browser and You will get this value in submit handler. see this code you will understand.

/**
 * Returns the form.
 */
function my_module_form() {
  return drupal_get_form('my_module_my_form');
}

/**
 * Returns the render array for the form.
 */
function my_module_my_form($form_state) {

  $form_state['storage']['product_nid'] = 12;

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

/**
 * Add a submit handler/function to the form.
 *
 * This will add a completion message to the screen when the
 * form successfully processes
 */
function my_module_my_form_submit($form, &$form_state) {
  $product_nid = $form_state['storage']['product_nid'];   
}
0
2

Use value instead, like this:

$form['product_nid'] = array(
 '#type' => 'value',
 '#value' => 12,
)

Elements of value type are never send to HTML, they are there just for what you are doing - passing values not meant to be changed by your user or scripts.

For complicated data, use answer by Akash Jain instead. Way he proposed is the only practical one for objects and arrays. On the other hand, for simple strings or integers it's too complicated. Especially if later they are meant to be treated in the same way as user input. Then, use value fields.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.