0

Now I have some variables collected from POST

$a = $_POST['a'];
$b = $_POST['b'];
$c = $a * $b

I need to use them in 2 functions: hook_form() and the callback mail_send() the function that format and sends my email with form elements.

I have implemented the following hooks:

function MYMODULE_page() {...}
function MYMODULE_form() {...}
function MYMODULE_form_submit($form, &$form_state) {
mail_send($form_state['values']);
}

//a custom callback mail function call in the submit.
function mail_send($form_values) {}

...

Now I don't know where to place those variables to have maximum visibility:

  • if I place them in MYMODULE_form() the show up in the form but they are not available in the mail_send().
  • if I place them outside everything they are not available inside functions.
  • if i place them inside a hook_init() the same as above.

I need a place where all variables are visible. Thanks

1

1 Answer 1

-1

For the maximum visibility use global variables like below

$a = $_POST['a'];
$b = $_POST['b'];
global $c;
$c = $a * $b

and you can anywhere like below,

function mail_send($form_values) {
  global $c;
  print_r($c);
}

There is no special methods exists in drupal, it is just like php.

4
  • 2
    Sorry, but I really need to down-vote a suggestion to use global. Use drupal_static instead.
    – Letharion
    Commented Feb 29, 2012 at 12:06
  • ohh sorry, i forgot that.But global also a correct suggestion, Please correct me If i am wrong? Commented Feb 29, 2012 at 12:11
  • 1
    It is absolutely correct, as in "It can solve the problem", yes. It's just that globals are generally considered a really bad idea, and should be avoided if you want your code to hold up to some level of scrutiny.
    – Letharion
    Commented Feb 29, 2012 at 12:16
  • Thank you for your explanation!. This is best suggestion why don't you add as next answer? Commented Feb 29, 2012 at 12:20

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