views:

118

answers:

5

Hello all,

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

A: 

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

jpabluz
Yes, I have - that didn't seem to do anything.
Abs
What about return false?
Chuck Conway
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
@Abs: they aren't entirely separate, `return false` would cancel the submit.
R0MANARMY
A: 

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();

Biff MaGriff
+1  A: 

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" />');
});
mVChr
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.
Abs
mmm your code is not behaving as properly, are you sure the form id is exactly "action_form" ???
jpabluz
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?
mVChr
+1  A: 

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(); 
});
Konerak
@Konerak - 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.
Abs
A: 
<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?

Sijin