In a drupal page, I have a form where I need to read a text field value of an element, via javascript, when it is typed by the user, just after the input field lost the focus and before submit is done, I need to save it in a variable.


All entered values are in the $form_state array on submit. It is up to you to process that data and store it in a variable.

For example...

function mymodule_myform_submit($form, $form_state) {
  $text = $form_state['values']['myfield'];
  variable_set('mymodule_var', $text);

Then wherever else in the site you want to show it...

$myvar = variable_get('mymodule_var', '');

Of course, be sure to validate your input in the validate handler and anything else you want to process and save. This is pretty much the most basic approach in 7 to collect and store variables (especially not knowing the rest of your setup).

| improve this answer | |
  • use variable_del('mymodule_var'); if you need to get rid off it after. – No Sssweat Feb 14 '17 at 0:24
  • I need to get the value inmediatly after the user type it, just after the input field lost the focus, and before submit button is clicked. I need to get the value via javascript to make some actions with this value. – bunset Feb 14 '17 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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