0

I was trying to submit a form using a javascript function,and below is my javascript function

   function submit_form(id_textarea,image_name_IN){
//var value_filled_textbox=document.getElementById(textarea_id_in).value;
var form=document.createElement("form_temp");//temporary dummy form
form.setAttribute("method","post");//its a post method
 form.setAttribute("action","comment.php"); // to which the page needs to be submitted

    var comment_value=document.getElementById(id_textarea).value; // gets the content of textarea
   // alert(comment_value);// just to make sure that you are getting the commeted text area content
     //create a hidden field for comments text
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", "comment_content");
          hiddenField.setAttribute("value", comment_value);

           form.appendChild(hiddenField);
        //   create a hidden field for passing image name
        var hiddenField2 = document.createElement("input");
            hiddenField2.setAttribute("type", "hidden");
            hiddenField2.setAttribute("name", "image_name" );
            hiddenField2.setAttribute("value",image_name_IN );

            form.appendChild(hiddenField2);

   document.body.appendChild(form);
    form.submit();
//alert(value_filled_textbox);
window.open("http://127.0.0.1/comment.php");
//document.getElementById('myform').submit();
}

while posting comment , I was getting two comment.php window,one with a result and the other with two warnings saying that undefined values "image_name" and "comment_content"

5
  • use jQuery submit api.jquery.com/submit to submit form. Commented Oct 1, 2013 at 9:39
  • What isn't a function? Commented Oct 1, 2013 at 9:40
  • im getting that submit.form() is not a function Commented Oct 1, 2013 at 9:40
  • 1
    submit.form() is not shown anywhere here. Commented Oct 1, 2013 at 9:40
  • @SubodhGhulaxe Don't, just don't. Commented Oct 1, 2013 at 9:46

3 Answers 3

2

Your form must be a <form> ... you used a non-existing HTML element <form_temp> which (of course) doesn't have a submit() method

var form=document.createElement("form");//temporary dummy form
Sign up to request clarification or add additional context in comments.

Comments

2

It's because form_temp is not a vaild form element. create a just a form element and it will get the submit function

var form=document.createElement("form");//temporary dummy form

Comments

0

USE

var comment_value=document.getElementById('id_textarea').value;

instead of

var comment_value=document.getElementById(id_textarea).value;

1 Comment

if i use var comment_value=document.getElementById('id_textarea').value; then it shows a error that document.getElementById is null

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.