# Remote\_form\_for behavior on javascript submit();

**URL:** <https://www.ruby-forum.com/t/remote-form-for-behavior-on-javascript-submit/109252>\
**Category:** Rails\
**Created:** [July 12, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/remote-form-for-behavior-on-javascript-submit/109252 "2007-07-12T19:20:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 12, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/remote-form-for-behavior-on-javascript-submit/109252/1 "2007-07-12T19:20:53Z")

</div>

Hey all,

Tried this on the IRC channel but after an hour i figured nobody that  
was on knew, so i’ll try it here. I have a rhtml page with:

\<% remote\_form\_for(:performance\_goal, :url =\>  
user\_performance\_goal\_path(@user.account, @user,  
@performance\_goal), :html =\> {:id =\> ‘sidebar\_form’, :method =\> :put})  
do |f| %\>

\<%= f.text\_area :impact, :size =\> ‘25x3’, :onchange =\>  
“this.form.submit();” %\>

\<% end %\>

please know that the :method =\> :put is in there to send the request  
to my RESTful update method. When i change the text area it does in  
fact submit the form, but not through AJAX. Instead it sends it as a  
normal html form and ignores the onsubmit attribute which reads:

onsubmit=“new Ajax.Request(’/accounts/1/users/1/gss/performance\_goals/  
1’, {asynchronous:true, evalScripts:true,  
parameters:Form.serialize(this)}); return false;”

Ive tried using the observe\_form helper and it behaves the same way.  
The only way i can have it submit through AJAX is by using a submit  
button. How can i submit the remote\_form\_for form via javascript and  
have it still perform the AJAX request?

Thanks!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 19, 2007, 1:14am UTC](https://www.ruby-forum.com/t/remote-form-for-behavior-on-javascript-submit/109252/2 "2007-07-19T01:14:54Z")

</div>

Hello Mpeychich,

This is not a rails problem nor a problem in your code. There is  
browser non-compliance to the submit() action which Steve Nyholm has  
documented here,  
[vertigosoftware.com](http://blogs.vertigosoftware.com/snyholm/archive/2006/09/27/3788.aspx),  
so what occurs is submit() does not call the onsubmit event handler  
that the ajax request is bound to.

I can see two potential solutions to this  
(1) do not call form.submit but instead generate the ajax on your own  
with something such as,  
“#{remote\_function(:url =\>user\_performance\_goal\_path(@user.account,  
@user, @performance\_goal))}; return false;”

(2) explicitly call the forms onsubmit event handler with some  
javascript

The problem with solution (1) is that this WILL NOT serialize your  
form elements, which is general something you want and often something  
you need. To serialize your form elements use the following,  
“#{remote\_function(:url =\>user\_performance\_goal\_path(@user.account,  
@user, @performance\_goal), :with =\> “Form.serialize(form)” )}; return  
false;”

where “form” in the serialization is your form.

Peter

On Jul 12, 10:20 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
