New Prototype User Problem....

0 views
Skip to first unread message

Joey H

unread,
Mar 24, 2008, 2:32:52 PM3/24/08
to Ruby on Rails: Spinoffs
Hello... New to prototype... I know this is probably just really
really simple but i have not been able to find a good tutorial
explination or documetation explination for how to do this... I Would
like this code to update the value of the cust_city form field...

thanks in advance for the help :o)

<CODE>

<script type="text/javascript" src="../prototype.js"></script>
<script>
function checkZip() {
if($F('cust_zipcode').length == 5) {

var url = 'ajax.zip_code.php';
var params = 'zip=' + $F('cust_zipcode');

var ajax = new Ajax.Updater(
{success: 'zipResult'},
url,
{method: 'post', parameters: params, onFailure: reportError}
);

}
}

function reportError(request) {
$F('zipResult') = "Error";
}
</script>

..
...

<input name="cust_zipcode" type="text" id="cust_zipcode" tabindex="6"
size="13" onkeyup="checkZip();" />

<input name="cust_city" type="text" id="cust_city" size="35" />



</CODE>

Diodeus

unread,
Mar 24, 2008, 3:25:18 PM3/24/08
to Ruby on Rails: Spinoffs
Since you're using Ajax.updater and not Ajax.Request, you need to
supply the ID of the element you wish to update.

Typically the syntax is as follows:

new Ajax.Updater(el, url, { method: 'get' });

el = ID of the element you're updating. In your case, use something
like <input id='city'>, so use 'city' as your ID in your call

url = if you're using GET you can pass the parameter in the URL, so in
your case the URL is:

'ajax.zip_code.php?zip=' + $F('cust_zipcode');

You can add the error handing if you want to.

Giv it a try.

Joey H

unread,
Mar 24, 2008, 3:48:20 PM3/24/08
to Ruby on Rails: Spinoffs
Hi Diodeus,

Can't seem to get it to work correctly... I can get it to appear in a
div tag though, i know that it's something simple that I am doing
wrong to make it show in a as the value of a text box.

Like this :

var url = 'ajax.zip_code.php';
var params = 'zip=' + $F('cust_zipcode');

var ajax = new Ajax.Updater('document.form1.cust_city.value',
url, {method: 'post', parameters: params}

or This

var url = 'ajax.zip_code.php';
var params = 'zip=' + $F('cust_zipcode');

var ajax = new Ajax.Updater('cust_city', url, {method: 'post',
parameters: params}

Thanks, Joe

Dan Dorman

unread,
Mar 24, 2008, 4:06:47 PM3/24/08
to rubyonrail...@googlegroups.com
On Mon, Mar 24, 2008 at 12:32 PM, Joey H <cjha...@gmail.com> wrote:
> I Would
> like this code to update the value of the cust_city form field...
>
> var ajax = new Ajax.Updater(
> {success: 'zipResult'},
> url,
> {method: 'post', parameters: params, onFailure: reportError}
> );

In order to update a form element's value, you'll need to use
Ajax.Request, _not_ Ajax.Updater; the latter works on a DOM element's
contents, that is, the HTML elements inside another HTML element. The
value of a form element, on the other hand, is set via its "value"
attribute.

So something more like this:

new Ajax.Request(url, {
method: 'post', // not necessary (POST's the default), but I'd use GET
parameters: params,
onSuccess: function(transport) {
$('cust_city').value = transport.responseText;
},
onFailure: reportError
});

:Dan

Diodeus

unread,
Mar 24, 2008, 4:20:35 PM3/24/08
to Ruby on Rails: Spinoffs
Duh!. Yup, Dan is right.

On Mar 24, 4:06 pm, "Dan Dorman" <dan.dor...@gmail.com> wrote:

Joey H

unread,
Mar 24, 2008, 4:44:08 PM3/24/08
to Ruby on Rails: Spinoffs
Thanks Guys for the Help... I hope I can return the favor soon!

Joe

On Mar 24, 3:06 pm, "Dan Dorman" <dan.dor...@gmail.com> wrote:

1969Benz

unread,
Apr 22, 2008, 10:08:43 PM4/22/08
to Ruby on Rails: Spinoffs
Hi guys,

Man, had to search for ages to find this solution :)

I would add that I couldn't get the proposed solution to work using:
"onSuccess: function(transport) {
$('cust_city').value = transport.responseText;
}, "

After looking up the Prototype site and the Ajax.Request function
here: http://www.prototypejs.org/api/form/element/getValue I had to
adapt the onSuccess function to this:


onSuccess: function(transport) {
var frm = $('formname');
var room = frm['room'];
$(room).value = transport.responseText;}

Small change, but caused a lot of frustration to find.

Cheers,
Ben.

jdalton

unread,
Apr 23, 2008, 12:18:09 AM4/23/08
to Ruby on Rails: Spinoffs
$('room').setValue( transport.responseText ); should work assuming the
element has the id 'room' and its unique
Reply all
Reply to author
Forward
0 new messages