2

I have a form with a submit button. I have attached a click event to that submit button using JQuery. Once I did this my form wasn't submitting, I thought this was because of the event I added- if I remove that click event it submits the form successfully.

I have tried giving my form an id and calling the submit function but this was not successful either. What can I do?

$('#submit_gen').click(function() {

  $('#genbtn').html('<img src="images/ajax-loader.gif" alt="loading" />');

  $('#action_form').submit(); 

});

The HTML:

<form id="action_form" action="generate.php" method="post" enctype="multipart/form-data"> 

<input id="file_upload" type="file" name="upload" onchange="file_select();"/>

<input type="text" id="overlay_input">

<div id="genbtn" class="genbtn">
<input id="submit_gen" type="submit" value="Upload">
</div>

</form>

Thanks all

3
  • Wait, so is that the working or the non-working code? And can you show the HTML? Commented May 21, 2010 at 22:07
  • I have added the HTML, the above does not work. Only the loading icon appears and nothing is submitted as the page just stays as it is. Commented May 21, 2010 at 22:09
  • Wouldn't your page go blank while the submit is in progress (as this is not an async submit? Commented May 21, 2010 at 22:31

5 Answers 5

3

I think a better way to do this would be to bind your custom code to the form submit itself, simply replace your code with:

$('#action_form').submit(function() {
  $('#genbtn').html('<img src="images/ajax-loader.gif" alt="loading" />');
});
Sign up to request clarification or add additional context in comments.

3 Comments

That's a good idea. But I think there is something wrong here. I tried the above (it should work and submit form) but it hasn't. Only the loader shows, and my firefox browser shows no indication of submitting the form. wtf is going on...Without any of these events the form submits normally.
mmm your code is not behaving as properly, are you sure the form id is exactly "action_form" ???
Well, that's what its id is in the example HTML. I've implemented exactly this behavior on a commercial site that's currently live, so I'm not sure why this isn't working for you. Perhaps the problem is something different?
2

It works in Opera, but Firefox indeed seems to refuse.

The problem appears to be the submitbutton in the genbtn div. If you hide the button, and insert the ajax-loader.gif AFTER the button, it does work. It does not work if you remove the submitbutton (replace the html), firefox appears to not submit the form anymore :)

$('#submit_gen').click(function() {
  $('#submit_gen').append('<img src="images/ajax-loader.gif" alt="loading" />');
  $('#submit_gen').hide();
  $('#action_form').submit(); 
});

1 Comment

thank you very much for highlighting that :). It's a behaviour of the firefox browser that I have never come across. Just to add, it works without the submit.
1

Have you tried return true; at the end of that anonymous function?

3 Comments

Yes, I have - that didn't seem to do anything.
Same thing with that too! I thought the click event was a separate thing to the submit event why are they even effecting each other?!
@Abs: they aren't entirely separate, return false would cancel the submit.
0

When stuff like this happen to me I make a 2nd invisible button that does the submitting. Something like this.

<input id="butInvis" style="display:none;" />

Then in my javascript function on the first button click I call butInvis.Click();

Comments

0
<form id="action_form" action="generate.php" method="post" enctype="multipart/form-data"> 

<input id="file_upload" type="file" name="upload" onchange="file_select();"/>

<input type="text" id="overlay_input">

<div id="genbtn" class="genbtn">
</div>

</form>

<input id="submit_gen" type="submit" value="Upload">

Does changing the html to above work?

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.