1

I am submitting a form using jQuery but when I use JavaScript it is submitted but through jQuery it is not submitting.

Here is my form code:

<form method="post" id="uploadfrm" action="<?php echo site_url('filemanager/upload'); ?>" enctype="multipart/form-data" >
<div class="up_wraper">Click here to Select a File </div>
<input type="file" name="upload[]" id="upload" multiple="multiple" /> </form>   

jQuery code:

<script type="text/javascript">
   $(document).ready( function() {
       $("#upload").change( function() { 
        var value=$("#foo").val();
        var file=$("#upload").val();
        var arr = value.split("_");
        var id = arr[1];
        var type = arr[0];

        if(type!='folder'){
            alert('Select a folder to upload files..!');
            }
          else{
                 $("#foo").val(id);
                 $('#uploadfrm').submit( function(event) {
                 event.preventDefault();

                 });

            }
       });
   });
</script>

When I use in else condition:

document.forms["uploadfrm"].submit();

it submits successfully.

But I want to submit it using jQuery using preventDefault() method.

4
  • 2
    So you want to submit a form, but prevent it from submitting? Commented Jul 23, 2013 at 10:47
  • Why you want event.preventDefault();it prevents from form submission Commented Jul 23, 2013 at 10:50
  • By using preventdefault form submit but its not refresh the browser window.. Commented Jul 23, 2013 at 10:50
  • Delete this line event.preventDefault(); and the form will submit i think. Commented Jul 23, 2013 at 10:54

2 Answers 2

1

In your code you have this...

$('#uploadfrm').submit( function(event) {
    event.preventDefault();
});

The 2nd line, event.preventDefault();, stops the form submitting. If you remove that it will submit fine. Just do this...

$('#uploadfrm').submit();
Sign up to request clarification or add additional context in comments.

3 Comments

@saqlain786 Huh? Not it isn't. preventDefault does exactly what it says on the tin. The event still occurs, but the form does not submit. Don't get them confused.
@ BenM hmmm..ok i am just trying..wait a moment.
saqlain786 - if this isn't what you need then give us a bit more info. The submit and refresh are the same thing.
0

You don't need event.preventDefault(); Simple $('#uploadfrm').submit(); triggers form submission. However, if you want to process some validations before submission, you can add another $.submit(); before your submission trigger.

    $('#uploadfrm').submit(function(){
      //your code goes here. 
     //return false; //if you want to break submission on failed validation, or if you want to use ajax callback
     //return true; // if you want to submit with regular form request. 
    });

   //this line bellow goes in your conditional, while upper one goes out of your $('#upload').change(); //event
   $('#uploadfrm').submit();

Comments

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.