1

I am trying to submit a form without page refresh or a submit button. But i have only achieved to have the JS function submit the input box value. Is it possible to submit the whole form without a button click and refresh of page?

JSFIDDLE

JS

            <script type="text/javascript">
                $(document).ready(function() {
                    var timer;
                        $('#yurl).on('keyup', function() {
                            var value = this.value;

                            clearTimeout(timer);

                            timer = setTimeout(function() {

                                //do your submit here

                                alert('submitted:' + value);
                            }, 2000);
                        });
                });
            </script>

html

                <form method="post" id="ytVideo" action="">
                <input id="yurl" type="text" value="<?php $url ?>" name="yurl">     
                </form>
1
  • I'm not sure if I understood your question correctly but based from what I understand you're trying to automatically submit a form once the time limit has been reached. Is that right? Commented Jul 13, 2012 at 1:52

3 Answers 3

1

If I understood well what you're asking, where it says //do your submit here you should put:

 $("#ytVideo").submit()

where ytVideo is the id of the form you're trying to submit

Good luck!

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

3 Comments

Yes when i change to the form id. I get in the alert: submitted: undefined
I tried it in the JSFiddle you provided, and it worked out fine for me.
and sorry, I messed up, it should be $("#ytVideo").submit(), not $("ytVideo").submit()
0

Your fiddle seems to be working but the problem is that you're not really submitting it anywhere.

<script type="text/javascript">
 $(document).ready(function() {
                        var timer;
                            $('#yurl).on('keyup', function() {
                                var value = this.value;

                                clearTimeout(timer);

                                timer = setTimeout(function() {

                                    //do your submit here
                                    $("#ytVideo").submit()
                                    alert('submitted:' + value);
                                }, 2000);
                            });


     //then include your submit definition. What you want to do once submit is executed
      $('#ytVideo').submit(function(e){
           e.preventDefault(); //prevent page refresh
           var form = $('#ytVideo').serialize();
           //submit.php is the page where you submit your form
           $.post('submit.php', form, function(data){ 
              //do something with the data

           });
      });

});
</script>

Comments

0

I think jQuery.serialize() could solve your problem quite nicely.

Comments

Your Answer

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