0

I know this has been asked many times but my code not working after looking at similar questions.

Form

 <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
    <div class="form-group">

    <input type="file" name="file" id="fileToUpload" required="">
    </div>
    <input type="submit" value="Upload Image" id="submit" name="submit" class="btn btn-info">
    <img src="authors/loading.gif" id="loading" style="max-height: 30px; width: auto; display: none;">
    </form>

Script

<script type="text/javascript">
$('#form1').on('submit', function(e) {
    e.preventDefault();

        $('#submit').hide();
        $('#loading').show();

    this.submit(); 
});

</script>

I am trying to hide submit button on click and then show a loading gif but it is directly submitting.

jQuery library all included and no error is showing in the console.

5
  • it is directly submitting - how did you know this? Even after you commenting out the this.submit()? Commented Apr 10, 2017 at 5:03
  • after show your triggering submit so it's submitting suddenly if you want see the gif change you can comment this.submit and try it Commented Apr 10, 2017 at 5:03
  • Try Placing Loading Img Out of the form .. Just a suggestion .. Commented Apr 10, 2017 at 5:04
  • @KingKing Yes even I comment this.submit() it is directly submitting Commented Apr 10, 2017 at 5:14
  • that's impossible, just tried your code, e.preventDefault() should work. You must have something wrong elsewhere making you think that the form is submitted anyway. Commented Apr 10, 2017 at 6:17

3 Answers 3

4

You should hide your submit and show your loading when the #submit is clicked, not when the form is submitted. And check the required fields before submitting. (But, this will require manual validation too.)

Change this $('#form1').on('submit', function(e) {

to $('#submit').on('click', function(e) {.

Working snippet below showing how you can toggle loading text / submit button based on form's required fields.

$('#submit').on('click', function(e) {
  e.preventDefault();
  $('form .error').remove();
  if($('#fileToUpload').val()!=""){
    console.log("file selected. submit the form now!");
    $('#submit').hide();
    $('#loading').show();
    $('#form1').submit(); 
  } else {
    console.log("file not selected. don't submit the form!");
    $('form').append("<div class='error'>file not selected</div>");
    $('#submit').show();
    $('#loading').hide();
  }    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
  <div class="form-group">
    <input type="file" name="file" id="fileToUpload" required="">
  </div>
  <input type="submit" value="Upload Image" id="submit" name="submit" class="btn btn-info">
  <img src="authors/loading.gif" id="loading" style="max-height: 30px; width: auto; display: none;">
</form>

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

4 Comments

Strangely its working here but when i copy page it it is still submitting. and no console output.
there must be something you are missing. are you sure it is the same as i've posted here?
Yes I even removed all my extra scripts and added yours and copied form and script from here but still its auto submitting.
try changing <input type="submit" value="Upload Image" id="submit" name="submit" class="btn btn-info"> to <button type="button" id="submit" name="submit" class="btn btn-info">Upload Image</button>
1

try this, may be this will help you

$('#form1').submit(function() {
    var pass = true;
    //some validations

    if(pass == false){
        return false;
    }
     $('#submit').hide();
    $('#loading').show();

    return true;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
    <div class="form-group">

    <input type="file" name="file" id="fileToUpload" required="">
    </div>
    <input type="submit" value="Upload Image" id="submit" name="submit" class="btn btn-info">
    <img src="http://tour.century21mcmullen.com/tour/titan/images/houseSpinner.gif" id="loading" style="max-height: 30px; width: auto; display: none;">
    </form>

1 Comment

returning «true or false» best solution if anyone want submit form again in function inner.
0

Now you can see the diffrents

After show your triggering submit so it's submitting suddenly if you want see the gif change you can comment this.submit and try it using setTimeout.

$('#form1').on('submit', function(e) {
    e.preventDefault();

        $('#submit').hide();
        $('#loading').show();
        
        setTimeout(function () {
                   // $(this).submit();
                    $('#loading').hide();
                     $('#submit').show();
                }, 5000); // in milliseconds
        
 });

  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
    <div class="form-group">

    <input type="file" name="file" id="fileToUpload" >
    </div>
    <input type="submit" value="Upload Image" id="submit" name="submit" class="btn btn-info">
    <img src="https://media4.giphy.com/media/xTk9ZvMnbIiIew7IpW/giphy.gif" id="loading" style="max-height: 30px; width: auto; display: none;">
    </form>

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.