JavaScript code call method in controller?

I have this text field that once a certain number of character are
entered I would like it to call a method in the controller and save the
data. To make a long story short, I do this because I don’t want the
user to hit a button to submit, but want this to be a quick edit option
for entering a telephone number.

What I am doing is to check the number of character in my javascript, if
the length equals the max I would like my method call to happen. Is
there a way to call a controller method from a javascript method?
Thanks,

-S

Shandy N. wrote:

I have this text field that once a certain number of character are
entered I would like it to call a method in the controller and save the
data. To make a long story short, I do this because I don’t want the
user to hit a button to submit, but want this to be a quick edit option
for entering a telephone number.

What I am doing is to check the number of character in my javascript, if
the length equals the max I would like my method call to happen. Is
there a way to call a controller method from a javascript method?
Thanks,

-S

I just did something like this today. In my case, when a certain event
happens, I fire off an AJAX call to update a session variable. Here’s
how I did it:

In my event code, I included a call to this function

function sidebar_menu_store_state(menu_id, expanded)
{
new Ajax.Request(’/person/store_sidebar_menu_state’, {
method: ‘post’,
parameters: {
menu_id: menu_id,
expanded: expanded
}
});
}

then in my controller, I have

def store_sidebar_menu_state
return unless request.xhr?
return unless request.post?

[do the logic]

render :nothing => true
end # store_sidebar_menu_state

There is probably a much better way to do it, but this works well for
what I need it to do. So if you already have the character count
happening, all you’d have to do is tack on the logic to hit the server
once the max is encountered.

I might add, though, that you could accomplish the same thing in an
onblur. When the user finishes typing in the text and focus leaves the
field, update the database.

Peace,
Phillip

On Wed, May 7, 2008 at 1:27 PM, Phillip K.
[email protected] wrote:

I might add, though, that you could accomplish the same thing in an
onblur. When the user finishes typing in the text and focus leaves the
field, update the database.

…which is probably better, or at least more universal, since a “phone
number” can be an arbitrary number of digits. :slight_smile:


Hassan S. ------------------------ [email protected]

Phillip K. wrote:

I might add, though, that you could accomplish the same thing in an
onblur. When the user finishes typing in the text and focus leaves the
field, update the database.

Peace,
Phillip

This is what I ended up doing and then submitting the data with a
remote_function call. Thanks,

-S