0

I have been working on this auto-login feature for my website. This feature includes an automatically sent form, created and submitted through pure javascript. When I try to send the form though, it gives me the error

Form submission canceled because the form is not connected

This is my code that's sending the form:

var method = "post";
var path = "cookie.php";
var form = document.createElement("form")
form.setAttribute("method",method);
form.setAttribute("path",path)
var hiddenform = document.createElement("input")
hiddenform.setAttribute("type","hidden")
hiddenform.setAttribute("id","hiddeninputusername")
hiddenform.setAttribute("value",cookieusername)
form.appendChild(hiddenform);
var hiddenforms = document.createElement("input")
hiddenforms.setAttribute("type","hidden")
hiddenforms.setAttribute("id","hiddeninputusername")
hiddenforms.setAttribute("value",cookiepassword)
form.appendChild(hiddenforms)
form.submit()

Note:This question is not a duplicate of the others because it is pure javascript

Sachi.Dila
  • 1,126
  • 7
  • 15
Dylan Ong
  • 786
  • 2
  • 6
  • 14
  • 1
    Possible duplicate of [Getting Error "Form submission canceled because the form is not connected"](https://stackoverflow.com/questions/42053775/getting-error-form-submission-canceled-because-the-form-is-not-connected) . It's still a duplicate IMO. JS or jQuery, the underlying issue is the same- your form is not part of the DOM. And the accepted answer in that link contains both native JS and jQuery versions of the solution. jQuery etc are just syntactic sugar on top of native JS. They can't do anything native JS can't do, and the rules of the game are still the same. – ADyson Aug 25 '18 at 06:58

1 Answers1

0

According to the HTML standatds, the error says that Form is not connected to the document context which means you need to append your form to the body element and then click the submit button.

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

This should work for you.

Saurabh Ghewari
  • 689
  • 3
  • 8