A question about submitting multiple parameters using Ajax.Updater

563 views
Skip to first unread message

kashyap

unread,
Oct 15, 2009, 6:49:10 AM10/15/09
to Prototype & script.aculo.us
Hi,
How can I use Ajax.Updater to send parameters from HTML - param1 and
param2 the values of which are not in a form as such using
Ajax.Updater in a manner such that on the server side, I use the
model's update_attributes method.

new Ajax.Updater(
'#{options[:update_html_id]}',
'#{options[:update_url]}'+id,
{asynchronous:true, evalScripts:true,
parameters:'authenticity_token=' +
encodeURIComponent('Hl7TS7DG2dM0zmGAO09HMU8rKTNJwto37APahAPK9vA=') }
)

How can I modify the Updater above to generate the data below so that
I can use model.udate_attributes(params[:some_model])

{ "authenticity_token"=>"Hl7TS7DG2dM0zmGAO09HMU8rKTNJwto37APahAPK9vA=",
"some_model"=>{"param1"=>"value1",
"param2"=>"value2"},
"id"=>"100"}


--
Regards,
Kashyap

Alex McAuley

unread,
Oct 16, 2009, 3:06:22 AM10/16/09
to prototype-s...@googlegroups.com
parameters : {

variable_1: 'Somevar',
variable_2: 'Some Other Var',
variable_3: 'Some third var....'

}

is that what you mean ?

Alex Mcauley
http://www.thevacancymarket.com

C K Kashyap

unread,
Oct 16, 2009, 4:17:28 AM10/16/09
to prototype-s...@googlegroups.com
On Fri, Oct 16, 2009 at 12:36 PM, Alex McAuley
<webm...@thecarmarketplace.com> wrote:
>
> parameters : {
>
> variable_1: 'Somevar',
> variable_2: 'Some Other Var',
> variable_3: 'Some third var....'
>
> }
>
> is that what you mean ?
>
Almost - except

parameters : {

variable_1: 'Somevar',
variable_2: {
a: 'hash members',
b: 'of',
c: 'variable 2'


},
variable_3: 'Some third var....'

}

A nested hash basically.

Regards,
Kashyap

Alex McAuley

unread,
Oct 16, 2009, 4:33:34 AM10/16/09
to prototype-s...@googlegroups.com
you will probably need to set variable 2 as a json object and decode it on
the server.

I have sent json objects like this before to the server and decoded them
back to an array once they arrive.

for example...
var data={};

for(i=0; i<=10;++i) {
data[i]=i;
}
var params=Object.toJSON(data);

var request=new Ajax.Request(.......

parameters : {
variable_1 : 'Hi i am variable one',
variable_2 : params // The json object
}
...........

HTH


Alex Mcauley
http://www.thevacancymarket.com


----- Original Message -----
From: "C K Kashyap" <ckka...@gmail.com>
To: <prototype-s...@googlegroups.com>
Sent: Friday, October 16, 2009 9:17 AM
Subject: [Proto-Scripty] Re: A question about submitting multiple parameters
using Ajax.Updater


>

C K Kashyap

unread,
Oct 17, 2009, 1:17:37 PM10/17/09
to prototype-s...@googlegroups.com
> for example...
> var data={};
>
> for(i=0; i<=10;++i) {
> data[i]=i;
> }
> var params=Object.toJSON(data);
>
> var request=new Ajax.Request(.......
>
> parameters : {
> variable_1 : 'Hi i am variable one',
> variable_2 : params // The json object
> }
> ...........

This did not quite work ... it looks like Object.toJSON returns a
string and on the server side what I get is

"variable_2" => "{\"data ...


Regards,
Kashyap

Alex McAuley

unread,
Oct 17, 2009, 3:12:29 PM10/17/09
to prototype-s...@googlegroups.com
it sends a json object to the server which you remove the slashes and
decode -

example in php

$posted=str_replace("\\"","\"",$__POST['varaible_2'];

$json=json_decode($posted);

// $json is now a multi dimensional array you can access

Alex Mcauley
http://www.thevacancymarket.com
----- Original Message -----
From: "C K Kashyap" <ckka...@gmail.com>
To: <prototype-s...@googlegroups.com>
Sent: Saturday, October 17, 2009 6:17 PM
Subject: [Proto-Scripty] Re: A question about submitting multiple parameters
using Ajax.Updater


>

Eric

unread,
Oct 27, 2009, 1:08:37 PM10/27/09
to Prototype & script.aculo.us
> $posted=str_replace("\\"","\"",$__POST['varaible_2'];

It is a little off-topic here, but the back-slashes are added or not
depending of PHP's configuration.

Here is the bullet-proof way to manage parameters in PHP:
$posted = $__POST['varaible_2'];
if (get_magic_quotes_gpc())
{
$posted = stripslashes($posted);
}

HTH,
Eric

On Oct 17, 8:12 pm, "Alex McAuley" <webmas...@thecarmarketplace.com>
wrote:
> it sends a json object to the server which you remove the slashes and
> decode -
>
> example in php
>
> $posted=str_replace("\\"","\"",$__POST['varaible_2'];
>
> $json=json_decode($posted);
>
> // $json is now a multi dimensional array you can access
>
> Alex Mcauleyhttp://www.thevacancymarket.com

Alex McAuley

unread,
Oct 27, 2009, 2:47:32 PM10/27/09
to prototype-s...@googlegroups.com
the other way is bullet proof lol....

If it finds a backslash quote it replaces it - if not it doesnt!!.

I agree that someone could send the string with a backslash quote in it but
its very unlikely!...

More than one way to skin a cat as they say ;)


Alex Mcauley
http://www.thevacancymarket.com
----- Original Message -----
Reply all
Reply to author
Forward
0 new messages