# Problem with auto-submitting AJAX form

**URL:** <https://www.ruby-forum.com/t/problem-with-auto-submitting-ajax-form/58887>\
**Category:** Rails\
**Created:** [April 26, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/problem-with-auto-submitting-ajax-form/58887 "2006-04-26T22:51:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [April 26, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/problem-with-auto-submitting-ajax-form/58887/1 "2006-04-26T22:51:53Z")

</div>

Hello,

I have a form generated with form\_remote\_tag that contains a selection  
list. I would like to auto-submit the form when the user makes a choice  
in the selection list, so I added an option :onchange =\>  
‘this.form.submit()’ to select\_tag

Now what happens is that when I make a choice, the partial template  
get’s rendered as expected, but not replacing the target div but the  
entire window instead! The div replacement works just fine when I submit  
the form using the submit button.

Why is that?

Ingo W.

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [April 26, 2006, 11:06pm UTC](https://www.ruby-forum.com/t/problem-with-auto-submitting-ajax-form/58887/2 "2006-04-26T23:06:18Z")

</div>

Ingo W. wrote:

> Hello,
> 
> I have a form generated with form\_remote\_tag that contains a selection  
> list. I would like to auto-submit the form when the user makes a choice  
> in the selection list, so I added an option :onchange =\>  
> ‘this.form.submit()’ to select\_tag
> 
> Now what happens is that when I make a choice, the partial template  
> get’s rendered as expected, but not replacing the target div but the  
> entire window instead! The div replacement works just fine when I submit  
> the form using the submit button.
> 
> Why is that?
> 
> Ingo W.

I’ve run into this before, the sad part is that I can’t remember how I  
solved it.

Your form is not being submitted via AJAX. All the magic of  
form\_remote\_tag happens in the onsubmit property of the form tag. The  
trouble is when the form is submit via javascript “form.submit()” the  
onsubmit is event is bypassed, yet it will work as expected via submit  
button. So when its submitted via javascript is just a standard form  
submission, with no AJAX.

Perhaps you can call “$(‘submit-button’).click” or even  
“this.form.onsubmit()” to directly call the events that make the magic  
happen.

I think that’s how I solved it anyway.

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [April 26, 2006, 11:42pm UTC](https://www.ruby-forum.com/t/problem-with-auto-submitting-ajax-form/58887/3 "2006-04-26T23:42:08Z")

</div>

> “this.form.onsubmit()”

This did the trick - thanks so much, Alex!

Ingo W.
