63

My sites Aweber registration forms are getting a lot of spam. I was told to create the forms dynamically via javascript after page has rendered or via clicking button. How would I create and render a form via javascript?

4
  • 1
    what do you mean by creating a form dynamically? Commented Aug 6, 2011 at 5:16
  • 6
    I'm not sure why this was downvoted so much or closed. Dynamic form generation via javascript means building the DOM rather than serving it via HTML. Commented Aug 6, 2011 at 5:22
  • 15
    He means creating a form after the page has been rendered. Why was this closed? Commented Aug 6, 2011 at 5:24
  • I wonder, is there a library for this, similar to the (PHP) Drupal Form API? Commented Jul 27, 2014 at 10:04

1 Answer 1

141

some thing as follows ::

Add this After the body tag

This is a rough sketch, you will need to modify it according to your needs.

<script>
var f = document.createElement("form");
f.setAttribute('method',"post");
f.setAttribute('action',"submit.php");

var i = document.createElement("input"); //input element, text
i.setAttribute('type',"text");
i.setAttribute('name',"username");

var s = document.createElement("input"); //input element, Submit button
s.setAttribute('type',"submit");
s.setAttribute('value',"Submit");

f.appendChild(i);
f.appendChild(s);

//and some more input elements here
//and dont forget to add a submit button

document.getElementsByTagName('body')[0].appendChild(f);

</script>
Sign up to request clarification or add additional context in comments.

2 Comments

How would you add a button (along side submit) to remove the respective form?
For some reason, the submit button for this code does not work in IE Edge, or previous versions of IE. Puzzled as to why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.