-1

Here I have made a form in javascript, Now I want to sumbit this form but I dont know how to submit this form.

Here is my little code any one can help me.?

<script>
    $("button#pass_data").on('click',function(e){                   

        var p_name = $(this).attr('value');

        '<form action="<?php echo base_url();?>teq" method="post" id="submit_papername">'+
            '<input typ="text" name="pip" value="'+p_name+'">'+
        '</form>'
        //document.getElementById('data_send').value == p_name;
        console.log('p_name');
        document.getElementById('submit_papername').submit();
        //alert(p_name);

});
</script>

I want to submit this newaly created form submit_papername , How can I do this.?

when I run above code it show me error like below.

TypeError: document.getElementById(...) is null

9
  • 1
    Why make the form at all? Just send an AJAX request. If you need to do it this way then you have to append the form to the DOM first. Commented Apr 21, 2017 at 10:40
  • 1
    You haven't actually made the form. You've defined a string, but you haven't done anything with it. Commented Apr 21, 2017 at 10:41
  • 1
    You have not appended String in DOM hence there is no form in DOM tree.. Commented Apr 21, 2017 at 10:41
  • 1
    What is the problem using AJAX? Commented Apr 21, 2017 at 10:42
  • 1
    @Rayon yes buddy thanks for this. I forget to append this. Commented Apr 21, 2017 at 10:42

1 Answer 1

0

You need to append element first $('body').append(app);.Then only perform the form submit

$("button#pass_data").on('click',function(e){                   

            var p_name = $(this).attr('value');

            var app = '<form action="<?php echo base_url();?>teq" method="post" id="submit_papername">'+
                '<input typ="text" name="pip" value="'+p_name+'">'+
            '</form>'
             $('body').append(app);
            //document.getElementById('data_send').value == p_name;
            console.log('p_name');
            document.getElementById('submit_papername').submit();
            //alert(p_name);

    });
Sign up to request clarification or add additional context in comments.

1 Comment

yes I forget to append this. Like I say it in above comments. Still I am accepting your answer. anyway thanks for help

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.