weyus
December 7, 2006, 11:22pm
1
If I write a GET AJAX request using remote function, how can I configure
it to pass the values of form fields.
Assume that I have a ZIP code field in a form, and I write something
like this:
remote_function(:url => {:action => ‘lookup_city_and_state’,
:object_name => object_name,
:check_state => check_state ? ‘1’ : ‘0’,
:mailing => mailing ? ‘1’ : ‘0’})
how would I pass the form value from my ZIP code field - or can it be
done?
I already have the Javascript version of this AJAX request working but
was wondering if I could use the remote_function idiom instead.
Thanks,
Wes
weyus
December 8, 2006, 12:25am
2
Wes,
Did you try the :with option?
e.g
:onclick => remote_function(
:url => {:action => :preview},
#:with => “‘name_of_form_field’=value_you_want_and_javascript_is_legal”
:with =>
“‘patch[editing_rdoc]=’+$(‘patch_modified_rdoc_source’).value”,
:before => “$(‘preview_target’).innerHTML=’…loading…’”,
:failure => “$(‘preview_target’).innerHTML=‘Ajax error!’”,
:update => ‘preview_target’
) + ‘;return false;’
–Jinal
weyus
December 8, 2006, 12:27am
3
Also you can use the submit option
<%= submit_tag ‘Save’, :name => ‘save’, :id => ‘save’ %>
<%= submit_tag ‘Update Preview’,
:name => ‘preview’,
:onclick => remote_function(
:url => {:action => :preview, :_method => :post},
:submit => “id_of_my_form”,
:before => “$(‘preview_target’).innerHTML=‘…loading…’”,
:failure => “$(‘preview_target’).innerHTML=‘Ajax error!’”,
:update => ‘preview_target’
) + ‘;return false;’
%>
Read http://www.timocracy.com/articles/tag/remote_function for detailed
information on various ways of doing this.
–Jinal Jhaveri
weyus
December 8, 2006, 7:55am
4
Jinal,
Thanks. I thought :with might work but didn’t see it in the API docs.
Generally, I find that finding a true comprehensive list of Prototype
API function parameters, or finding a comprehensive list of RoR AJAX-y
function parameters can be somewhat challenging.
I wonder if anyone is working on a complete Prototype/Script.aculo.us
reference?
Anyhow, thanks again, all works great.
Wes