0

I have multiple pages which contains multiple ajax forms. When I am working on a particular form, its ajax submission is working. But at that moment, if I go to second page form, ajax submission not working and the page gets reloaded.

But after clearing cache, if I work on second page form, its ajax submission working, but then first page ajax form submission not working.

How will I manage multiple ajax forms on multiple pages?

My ajax submission code looks like this:

$form['actions']['submit'] = array(
  '#type' => 'submit',
  '#value' => $this->t('Submit'),
  '#attributes' => array('class' => array('btn-red')),
  '#ajax' => [
    'callback' => '::submitCallBack',
    'progress' => array(
      'type' => 'throbber',
      'message' => 'Please wait...',
    ),
  ],
);
4
  • 1
    I think you need to show more of your code. Otherwise it's difficult to help you.
    – berliner
    Commented Jun 3, 2022 at 8:10
  • How do you switch to the second page, is this also Ajax?
    – 4uk4
    Commented Jun 3, 2022 at 8:12
  • @4uk4 no, its a simple another node
    – Souvik Das
    Commented Jun 3, 2022 at 8:16
  • 1
    OK, please add the code how you embed the form in the node.
    – 4uk4
    Commented Jun 3, 2022 at 9:06

1 Answer 1

0

After a lot of research I found that there is a confliction issue having same name of the submit button for all of the forms. So I changed the submit button name for each forms.

Now the ajax is working fine for all forms.

Form 1 submit button:

$form['form_one_submit'] = [
  '#type' => 'submit',
  '#value' => $this->t('Submit'),
  '#ajax' => [
    'callback' => '::callBackFunctionOne',
    'progress' => array(
      'type' => 'throbber',
      'message' => 'Just a moment...',
    ),
  ],
];

Form 2 submit button:

$form['form_two_submit'] = [
  '#type' => 'submit',
  '#value' => $this->t('Submit'),
  '#ajax' => [
    'callback' => '::callBackFunctionTwo',
    'progress' => array(
      'type' => 'throbber',
      'message' => 'Please wait...',
    ),
  ],
];

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.