1

I want to change the value of the submit button after the the form is submitted successfully without any validation errors.

And then when the page is reloaded the button's value will be changed to its original value?

I think its not possible with any javascript or jquery but I can't find a value in the $form_state array to set a js variable.

Edit: The form is submitted without ajax. The button should be replaced with that text after the form is submitted. And then when the user comes back to page again or reload it again then the text should be reverted.

2
  • Can you edit your post to give more information on what you are trying to do? Is the form submitted with AJAX? Do you want the button to change while the form is submitting? Or after the form submits and the page re-loads, the button text should be different, and if the user leaves the page and comes back (or reloads the page), the text will revert? We need to know a specific use case of what you are trying to do.
    – Jaypan
    Commented Feb 9, 2017 at 8:58
  • @Jaypan I have edited my question.
    – Ahmad
    Commented Feb 9, 2017 at 9:35

1 Answer 1

2

You can do this with two steps. The first is to check whether $form_state['values'] has been set, and change the value accordingly:

if(!isset($form_state['values'])) {
  $form['submit'] = array (
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
}
else {
  $form['submit'] = array (
    '#type' => 'submit',
    '#value' => t('Submit Again'),
  );
}

The next step is in your submit function, where you have to tell Drupal to rebuild the form (rather than build it from scratch)

$form_state['rebuild'] = TRUE;
3
  • with $form_state['rebuild'] = TRUE, the drupal.org/project/nocurrent_pass is not working anymore. Before that it was working. Displaying this error The form has become outdated. Copy any unsaved work in the form below and then reload this page.
    – Ahmad
    Commented Feb 9, 2017 at 12:11
  • Where did you put it?
    – Jaypan
    Commented Feb 9, 2017 at 14:07
  • it is fixed by placing the $form_state['rebuild'] = TRUE; $form_state['input']['form_token'] = drupal_get_token($form['#token']); in the submit function
    – Ahmad
    Commented Feb 9, 2017 at 15:25

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.