Getting Error "Form submission canceled because the form is not connected"

JavascriptJqueryGoogle ChromeJquery 1.7

Javascript Problem Overview


I have an old website with JQuery 1.7 which works correctly till two days ago. Suddenly some of my buttons do not work anymore and, after clicking on them, I get this warning in the console:

> Form submission canceled because the form is not connected

The code behind the click is something like this:

 this.handleExcelExporter = function(href, cols) {
   var form = $('<form method="post"><input type="submit" /><input type="hidden" name="layout" /></form>').attr('action', href);
   $('input[name="layout"]', form).val(JSON.stringify(cols));
   $('input[type="submit"]', form).click();
 }

It seems that Chrome 56 doesn't support this kind of code anymore. Isn't it? If yes my question is:

  1. Why did this happened suddenly? Without any deprecation warning?
  2. What is the workaround for this code?
  3. Is there a way to force chrome (or other browsers) to work like before without changing any code?

P.S. It doesn't work in the latest firefox version either (without any message). Also it does not work in IE 11.0 & Edge! (both without any message)

Javascript Solutions


Solution 1 - Javascript

Quick answer : append the form to the body.

document.body.appendChild(form);

Or, if you're using jQuery as above: $(document.body).append(form);

Details : According to the HTML standards, if the form is not associated to the browsing context(document), the form submission will be aborted.

HTML SPEC see 4.10.21.3.2

In Chrome 56, this spec was applied.

Chrome code diff see @@ -347,9 +347,16 @@

P.S about your question #1. In my opinion, unlike ajax, form submission causes instant page move.
So, showing 'deprecated warning message' is almost impossible.
I also think it's unacceptable that this serious change is not included in the feature change list. Chrome 56 features - www.chromestatus.com/features#milestone%3D56

Solution 2 - Javascript

if you are seeing this error in React JS when you try to submit the form by pressing enter, make sure all your buttons in the form that do not submit the form have a type="button".

If you have only one button with type="submit" pressing Enter will submit the form as expected.

References:
https://dzello.com/blog/2017/02/19/demystifying-enter-key-submission-for-react-forms/ https://github.com/facebook/react/issues/2093

Solution 3 - Javascript

add attribute type="button" to the button on who's click you see the error, it worked for me.

Solution 4 - Javascript

alternatively include event.preventDefault(); in your handleSubmit(event) {

see https://facebook.github.io/react/docs/forms.html

Solution 5 - Javascript

You must ensure that the form is in the document. You can append the form to the body.

Solution 6 - Javascript

I see you are using jQuery for the form initialization.

When I try @KyungHun Jeon's answer, it doesn't work for me that use jQuery too.

So, I tried appending the form to the body by using the jQuery way:

$(document.body).append(form);

And it worked!

Solution 7 - Javascript

A thing to look out for if you see this in React, is that the <form> still has to render in the DOM while it's submitting. i.e, this will fail

{ this.state.submitting ? 
     <div>Form is being submitted</div> :
     <form onSubmit={()=>this.setState({submitting: true}) ...>
         <button ...>
     </form>
}

So when the form is submitted, state.submitting gets set and the "submitting..." message renders instead of the form, then this error happens.

Moving the form tag outside the conditional ensured that it was always there when needed, i.e.

<form onSubmit={...} ...>
  { this.state.submitting ? 
     <div>Form is being submitted</div> :
     <button ...>
  }
</form>

Solution 8 - Javascript

I have found this problem in my React project.

The problem was,

  • I have set the button type 'submit'
  • I have set an onClick handler on the button

So, while clicking on the button, the onclick function is firing and the form is NOT submitting, and the console is printing - > Form submission canceled because the form is not connected

The simple fix is:

  • Use onSubmit handler on the form
  • Remove the onClick handler form the button itself, keep the type 'Submit'

Solution 9 - Javascript

<button type="button">my button</button>

we have to add attribute above in our button element

Solution 10 - Javascript

I faced the same issue in one of our implementation.

we were using jquery.forms.js. which is a forms plugin and available here. http://malsup.com/jquery/form/

we used the same answer provided above and pasted

$(document.body).append(form);

and it worked.Thanks.

Solution 11 - Javascript

I was able to get rid of the message by using adding the attribute type="button" to the button element in vue.

Solution 12 - Javascript

Depending on the answer from KyungHun Jeon, but the appendChild expect a dom node, so add a index to jquery object to return the node: document.body.appendChild(form[0])

Solution 13 - Javascript

Adding for posterity since this isn't chrome related but this was the first thread that showed up on google when searching for this form submission error.

In our case we attached a function to replace the current div html with a "loading" animation on submission - since it occurred before the form was submitted there was no longer any form or data to submit.

Very obvious error in retrospect but in case anyone ends up here it might save them some time in the future.

Solution 14 - Javascript

I have received this error in react.js. If you have a button in the form that you want to act like a button and not submit the form, you must give it type="button". Otherwise it tries to submit the form. I believe vaskort answered this with some documentation you can check out.

Solution 15 - Javascript

An example of Mike Ruhlin's answer, I was redirecting with react-router-dom Redirect on form submission.

Placing e.preventDefault() into my submit function removed the warning for me

const Form = () => {

    const [submitted, setSubmitted] = useState(false);
    const submit = e => {
      e.preventDefault();
      setSubmitted(true);
    }

    if (submitted) {
        return <Redirect push to={links.redirectUrl} />
    };

    return (
          <form onSubmit={e => submit(e)}>
          ...
          </form>

    );
};

export default Form;

Solution 16 - Javascript

You can also solve it, by applying a single patch in the jquery-x.x.x.js just add after " try { rp; } catch (m) {}" line 1833 this code:

if (r instanceof HTMLFormElement &&! r.parentNode) { r.style.display = "none"; document.body.append (r); r [p] (); }

This validates when a form is not part of the body and adds it.

Solution 17 - Javascript

I noticed that I was getting this error, because my HTML code did not have <body> tag.

Without a <body>, when document.body.appendChild(form); statement did not have a body object to append.

Solution 18 - Javascript

I saw this message using angular, so i just took method="post" and action="" out, and the warning was gone.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMahmoud MoravejView Question on Stackoverflow
Solution 1 - JavascriptKyungHun JeonView Answer on Stackoverflow
Solution 2 - JavascriptvaskortView Answer on Stackoverflow
Solution 3 - JavascriptTayyab MeharView Answer on Stackoverflow
Solution 4 - JavascriptjoncodeView Answer on Stackoverflow
Solution 5 - JavascriptLiu TaoView Answer on Stackoverflow
Solution 6 - JavascriptRizki PratamaView Answer on Stackoverflow
Solution 7 - JavascriptMike RuhlinView Answer on Stackoverflow
Solution 8 - JavascriptSadat JubayerView Answer on Stackoverflow
Solution 9 - JavascriptBalanagu YashwanthView Answer on Stackoverflow
Solution 10 - JavascriptAbhinav ChawlaView Answer on Stackoverflow
Solution 11 - JavascriptPasham Akhil Kumar ReddyView Answer on Stackoverflow
Solution 12 - Javascriptmoti iromView Answer on Stackoverflow
Solution 13 - JavascriptMatt HView Answer on Stackoverflow
Solution 14 - JavascriptIsaiah LarsenView Answer on Stackoverflow
Solution 15 - JavascriptSamView Answer on Stackoverflow
Solution 16 - JavascriptGaytanView Answer on Stackoverflow
Solution 17 - JavascriptGlenoView Answer on Stackoverflow
Solution 18 - JavascriptheavyrickView Answer on Stackoverflow