3

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.

1 Answer 1

2

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).

2
  • use variable_del('mymodule_var'); if you need to get rid off it after.
    – No Sssweat
    Commented Feb 14, 2017 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
    Commented Feb 14, 2017 at 6:09

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.