4 Replies - 428 Views - Last Post: 04 December 2012 - 08:16 AM

#1 tanuja90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-December 12

Unable to submit dynamically generated form

Posted 04 December 2012 - 07:05 AM

I have written a javascript function to generate and submit a dynamic form. But, I'm not able to submit it. Here's my code. The function below is called by click on a button. The form is submitted to the same page where the button is placed. i.e., to index.php.
Any help will be highly appreciated.

function dynaForm(){
	
	var new_form = document.createElement("form");
	new_form.method = "POST";
	new_form.action = "../index.php";
		
	var actionField = document.createElement("input");
	actionField.name = "act";
	actionField.type = "hidden";
	actionField.value = "update";
	
	new_form.appendChild(actionField);

	new_form.submit();
}


Is This A Good Question/Topic? 0
  • +

Replies To: Unable to submit dynamically generated form

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Unable to submit dynamically generated form

Posted 04 December 2012 - 07:10 AM

Hello,

It appears that although you create the form element and inject a button, you don't actually add it to the document at any point. I may be mistaken but I think you need to put the form somewhere before submitting it will work.

Try adding the following before you call .submit()
document.appendChild(new_form);


( If I'm having a derp and you can't call appendChild from the document, try adding it to the body )
Was This Post Helpful? 1
  • +
  • -

#3 tanuja90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-December 12

Re: Unable to submit dynamically generated form

Posted 04 December 2012 - 07:40 AM

Thank you for the reply. :bigsmile:/>/> Added form tag to the body and generated the form elements dynamically. And,it worked. Thanks a lot.

This post has been edited by Dormilich: 04 December 2012 - 07:59 AM
Reason for edit:: removed quote

Was This Post Helpful? 0
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Unable to submit dynamically generated form

Posted 04 December 2012 - 07:42 AM

Glad I could be of assistance, remember to click the little green plus icon if a post was helpful for you!
Was This Post Helpful? 0
  • +
  • -

#5 tanuja90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-December 12

Re: Unable to submit dynamically generated form

Posted 04 December 2012 - 08:16 AM

done :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1