# RE: Update multiple fields in a form on change event

**URL:** <https://www.ruby-forum.com/t/re-update-multiple-fields-in-a-form-on-change-event/48481>\
**Category:** Rails\
**Created:** [November 17, 2005, 3:17pm UTC](https://www.ruby-forum.com/t/re-update-multiple-fields-in-a-form-on-change-event/48481 "2005-11-17T15:17:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![torben.wolm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@torben.wolm](https://www.ruby-forum.com/u/torben.wolm)\
**Post date:** [November 17, 2005, 3:17pm UTC](https://www.ruby-forum.com/t/re-update-multiple-fields-in-a-form-on-change-event/48481/1 "2005-11-17T15:17:20Z")

</div>

Hi Damon,

Do you have some code to show? That would make things a bit easier to  
comment on…

Best regards,  
torben

---

<div class="post-metadata">

**Author:** ![torben.wolm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@torben.wolm](https://www.ruby-forum.com/u/torben.wolm)\
**Post date:** [November 17, 2005, 3:44pm UTC](https://www.ruby-forum.com/t/re-update-multiple-fields-in-a-form-on-change-event/48481/2 "2005-11-17T15:44:29Z")

</div>

Torben–

Thanks for the reply.  
I do have some code that I posted yesterday in another thread, but would  
love to post here again for you 🙂

Main View

Customer Address:

Ship Via:

&nbsp; Credit Limit:

Current Bal:

\<%= observe\_field(:purchase\_order\_customer\_id, :update =\> :po\_customer\_address, :url =\> { :action =\> :get\_customer\_address }, :with =\> "'customer\_id='+escape(value)")%\> \<%= observe\_field(:purchase\_order\_customer\_id, :update =\> :po\_credit\_limit, :url =\> { :action =\> :get\_customer\_credit\_limit }, :with =\> "'customer\_id='+escape(value)")%\>

Controller

def get\_customer\_address  
form\_customer\_id = params[:customer\_id]  
@found\_customer = Customer.find(form\_customer\_id)  
end

def get\_customer\_credit\_limit  
form\_customer\_id = params[:customer\_id]  
@found\_customer\_limit = Customer.find(form\_customer\_id)  
end

Returning Views

get\_customer\_address.rhtml  
\<%= text\_field(“purchase\_order”, “customer\_address”, :readonly =\>  
“readonly”, “size” =\> 25, :value =\> @found\_customer.address) %\>  
  
\<%= text\_field(“purchase\_order”, “customer\_city”, :readonly =\>  
“readonly”,  
“size” =\> 10, :value =\> @found\_customer.city) %\>  
\<%= text\_field(“purchase\_order”, “customer\_state”, :readonly =\>  
“readonly”,  
“size” =\> 2, :value =\> @found\_customer.state) %\>  
\<%= text\_field(“purchase\_order”, “customer\_postal\_code”, :readonly =\>  
“readonly”, “size” =\> 5, :value =\> @found\_customer.postal\_code) %\>

get\_customer\_credit\_limit.rhtml  
\<%= text\_field(“purchase\_order”, “customer\_credit\_limit”, :readonly =\>  
“readonly”, :value =\> @found\_customer\_limit.credit\_limit) %\>

Thanks!  
~damon

---

<div class="post-metadata">

**Author:** ![torben.wolm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@torben.wolm](https://www.ruby-forum.com/u/torben.wolm)\
**Post date:** [November 17, 2005, 8:22pm UTC](https://www.ruby-forum.com/t/re-update-multiple-fields-in-a-form-on-change-event/48481/3 "2005-11-17T20:22:48Z")

</div>

Hi Damon

I can’t say that I have tried the thing you want, but I had a look in  
the “Agile Development”-book that holds a tip I think might be usable.

The tip goes, that you return a bunch of generated javascript that you  
evaluate in the client.

So, you will do something like:

observe\_field(:purchase\_order\_customer\_id,  
:complete =\> “eval(request.responseText)”,  
:url =\> { :action =\> :get\_all\_customer\_info },  
:with =\> “‘customer\_id=’+escape(value)”)

In the template, you generate the javascript that updates all the  
fields:

document.getElementById(“po\_customer\_address”).value = ‘\<%=  
@found\_customer.address %\>’;  
document.getElementById(“po\_credit\_limit”).value = ‘\<%=  
@found\_customer\_limit.credit\_limit %\>’;

and so on.

Hope this helps…

Torben

---

<div class="post-metadata">

**Author:** ![torben.wolm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@torben.wolm](https://www.ruby-forum.com/u/torben.wolm)\
**Post date:** [November 17, 2005, 8:37pm UTC](https://www.ruby-forum.com/t/re-update-multiple-fields-in-a-form-on-change-event/48481/4 "2005-11-17T20:37:52Z")

</div>

El Jueves 17 Noviembre 2005 19:20, Torben  
Wölm escribió:\> observe\_field(:purchase\_order\_customer\_id,

> @found\_customer\_limit.credit\_limit %\>';  
> I think that you have an “update\_element\_function”[1] to do such a  
> thing.

Good luck!

[1]  
[http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#M000430](http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#M000430)

–  
_o) Imobach González Sosa  
//\ email: imobachgs at step dot es  
V_/\_ jid: osoh at jabberes dot org  
url: [banot.net&nbsp;-&nbsp;banot Resources and Information.](http://www.banot.net/~osoh/)  
blog: [http://devnull.blogs.banot.net/](http://devnull.blogs.banot.net/)

* * *

[http://www.step.es/](http://www.step.es/)

–  
Este mensaje ha sido analizado por STEP On Line  
en busca de virus y otros contenidos peligrosos,  
y se considera que está limpio. 902 10 18 43

---

<div class="post-metadata">

**Author:** ![torben.wolm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@torben.wolm](https://www.ruby-forum.com/u/torben.wolm)\
**Post date:** [November 17, 2005, 8:46pm UTC](https://www.ruby-forum.com/t/re-update-multiple-fields-in-a-form-on-change-event/48481/5 "2005-11-17T20:46:55Z")

</div>

> I think that you have an “update\_element\_function”[1] to do such a thing.
> 
> Good luck!

> [1]  
> [ActionView::Helpers::JavaScriptHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/JavaScriptHelper.html#M000430)

Ahh - sweet. The documentation even holds an example of – how to make  
multiple updates!

Thanks,  
Torben

---

<div class="post-metadata">

**Author:** ![torben.wolm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@torben.wolm](https://www.ruby-forum.com/u/torben.wolm)\
**Post date:** [November 17, 2005, 9:11pm UTC](https://www.ruby-forum.com/t/re-update-multiple-fields-in-a-form-on-change-event/48481/6 "2005-11-17T21:11:06Z")

</div>

Thanks for the replies Torben and Imobach.  
I did attempt to implement the update\_element\_function however, the  
action  
it was looking for was an onSubmit() and I need the form to update on an  
onEvent() action.

I will look again and see if there is a way to set which action the  
update\_element\_function looks for.

Thanks again,  
~damon

---

<div class="post-metadata">

**Author:** ![torben.wolm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bbce88/32.png) [@torben.wolm](https://www.ruby-forum.com/u/torben.wolm)\
**Post date:** [November 17, 2005, 10:26pm UTC](https://www.ruby-forum.com/t/re-update-multiple-fields-in-a-form-on-change-event/48481/7 "2005-11-17T22:26:33Z")

</div>

Solved!!!

The examples and hints given by Torben worked beautifully! Thanks!

Here is my view now:

Customer Address: Shipper Address:

&nbsp; Credit Limit: Current Bal: Customer Pickup:\</label \<%= check\_box\_tag("po\_customer\_pickup", value="1", checked=false) %\>

\<%= observe\_field(:purchase\_order\_customer\_id,  
:complete =\> “eval(request.responseText)”,  
:url =\> { :action =\> :get\_customer\_info },  
:with =\> “‘customer\_id=’+escape(value)”)%\>

My controller just finds the specified customer record designated by  
customer\_id.

Then the get\_customer\_info.rhtml looks like this:

document.getElementById(“po\_customer\_address”).value = ‘\<%=  
@found\_customer.address %\>’;  
document.getElementById(“po\_customer\_city”).value = ‘\<%=  
@found\_customer.city %\>’;  
document.getElementById(“po\_customer\_state”).value = ‘\<%=  
@found\_customer.state %\>’;  
document.getElementById(“po\_customer\_postal\_code”).value = ‘\<%=  
@found\_customer.postal\_code %\>’;  
document.getElementById(“po\_credit\_limit”).value = ‘\<%=  
@found\_customer.credit\_limit %\>’;  
document.getElementById(“po\_current\_balance”).value = ‘\<%=  
@found\_customer.inv\_balance %\>’;

Thanks again!
