parameters in ajax.request()

Hi,
I am looking an ajax.request somthingl ike shown in code section

there are two thing I want to ask,
1. how do we add the parameters. If I look at the ajax options,
I understand that we can send the parameters : ' ' but how ?
would it be somthing like parameters : ' tets = ... , test2 = ... '
and then how owuld I retrieve then in .php
$_post('test')?
Please guied

2.
what exactly the function(t) doing here
I can sort of understand but it would be ideal to have an expert explaining it :)

Thanks  
// save it to the database
					new Ajax.Request(this.options.saveurl, { method: 'post', postBody: postBody, onComplete : function (t) {
						$('data').update(t.responseText + $('data').innerHTML);
					} });

Open in new window

SameerMirzaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mydropzCommented:
I'm not sure how the postBody takes parameters but when you use the parameters option as shown below
you can retrieve the values with php using $_POST['param1']
// save it to the database
					new Ajax.Request(this.options.saveurl, { method: 'post', parameters: {param1: 'value_1', param2:'value2'}, onComplete : function (t) {
						$('data').update(t.responseText + $('data').innerHTML);
					} });

Open in new window

when you send values form a form you could replace the parameters with
 parameters: $('id_of_form_element').serialize(true)

Open in new window

the function(t) declares a function that needs to be executed OnComplete

SameerMirzaAuthor Commented:
ok. for the axaj request in OP.
If I had to send some test parameters
I would say,
parameters : {login: 'somelogin'},

which I am assuming that I should be able to retrieve by saying,
$login = $_POST['login']; ?

Unfortunatly its not working for me.
Can any one please let me know where m I going wrong. Whereas it does the rest jusrt fine - means no errors
// save it to the database
					new Ajax.Request(this.options.saveurl, { method: 'post', postBody: postBody, parameters : {login: 'somelogin'}, onComplete : function (t) {
						$('data').update(t.responseText + $('data').innerHTML);
					} });

Open in new window

mydropzCommented:
when using method:post and sending parameters don't use postbody
// save it to the database
new Ajax.Request(this.options.saveurl, { method: 'post', parameters : {login: 'somelogin'}, onComplete : function (t) {
						$('data').update(t.responseText + $('data').innerHTML);
					} });]

Open in new window


because when postbody is set the parameters are ignored
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

te-eduCommented:
/ save it to the database
postBody = 'data_1'+variable_with_data+'&data_2'+variable_2;
or

postBody = postBody &'data_1'+variable_with_data+'&data_2'+variable_2;

new Ajax.Request(this.options.saveurl, { 
method: 'post', 
postBody: postBody, 
onComplete : function (t) {
						$('data').update(t.responseText + $('data').innerHTML);
					} });

Open in new window



postBody = postBody &'data_1'+variable_with_data+'&data_2'+variable_2;
SameerMirzaAuthor Commented:
Hi,
thanks alot for help. The data is sent as shown above but please have a look at how its retrieved.
It will be great if some one could tell me how would I post/retrieve
 parameters : {login : "somelogin"}
I dont mind which ever way you like to do it. Either as part of postbody of parameter.

thanks
$data = $_POST;

foreach ($data as $row) 
{
	/* split up in segments */
	$segments = explode(':', $row);
	
	/* define the column */
	$column_id = $segments[0];
	
	/* the blocks */
	$blocks = explode(',', $segments[1]);
	
	/* we take each block */
	foreach ($blocks as $order_id => $block_id)
	{
		/* check if the block is already present in the database */
		$block_exists = mssql_fetch_row(mssql_query("SELECT * FROM blocks WHERE block_id = '{$block_id}'"));
		
		/* if not, we insert it */
		if ($block_exists == FALSE) 
		{
			if (empty($block_id)) return;
			
			mssql_query("INSERT INTO blocks (block_id, column_id, order_id) VALUES ('{$block_id}', '{$column_id}', {$order_id})");
			echo "Moved block: {$block_id} to column: {$column_id} and updated rank to: {$order_id}<br />";
		}
		/* or else we update it */
		else 
		{
			if (empty($block_id)) return;
			
			mssql_query("UPDATE blocks SET block_id = '{$block_id}', column_id = '{$column_id}', order_id = {$order_id} WHERE unique_id = ".$block_exists[0]);
			echo "Moved block: {$block_id} to column: {$column_id} and updated rank to: {$order_id}<br />";
		}
	}
	
}

Open in new window

mydropzCommented:
a parameter posted as {'login': 'somelogin'}
can be retrieved by using $_POST['login'] so in your example you could use
$data = $_POST;

foreach ($data as $key => $value) 
{
	/* split up in segments */
	// not needed any more
	
	/* define the column */
	$column_id = $key;
	
	/* the blocks */
	$blocks = explode(',', $value); // this will only work when the value is an array
	
	/* we take each block */
	foreach ($blocks as $order_id => $block_id) // this will fail if your value is not an array 
	{
	

Open in new window

i only posted the code that i changed
SameerMirzaAuthor Commented:
parameterz dont work when you have set the postbody..
you actually confirmed it in the previous post and I experienced it :)
mydropzCommented:
ok but the retreiving of the values made sense ???
SameerMirzaAuthor Commented:
yes thanks. Its very basic, I know it already. As a php developer (may be very junior) you use it all the time :)

anyways.. do you know how can I actually see the wats in postbody?
its part of a class and works on event. I can guess what its passing but how would track it
cant even use alert or anything in this case
mydropzCommented:
if you use firefox with the firebug plugin and you enable the console window all the ajax request will show up in that window with the information on the parameter(postbody) that is sent and the return of your script
SameerMirzaAuthor Commented:
ok great. that helps, I can the post and response values
but how do I send the login name :/

postbody doesnt take anything undefined. Isnt there any simple way to saperatly send the login name?
mydropzCommented:
I don't understand what you mean :(

do you mean that you have a form with the login information? in that case you can use form.serialize() to send the values of the form i'l give an example
<form id="form_id">
<input type="text" name="login" id="login"/>
<input type="password" name="pass" id="pass />
<input type="submit" />
// a very simple form

new Ajax.Request(this.options.saveurl, { method: 'post', postBody: $('#form_id').serialize(), onComplete : function (t) {
						$('data').update(t.responseText + $('data').innerHTML);
					} });

Open in new window


Now in the postBody parameter i used the form.serialize function of jquery it will prepare all the form values to be sent to you script so in php you can retrieve them by
$_POST['login'] //value of the field with name login
$_POST['pass'] //value of the field with name pass

Open in new window


if you want the complete documentation on the form.serialize function check here
SameerMirzaAuthor Commented:
I completly understand what you mean.
But this is a very different senario, I am not using form all

Infact I am using some one code who created drag and dropable divs. which save the state in db
What I am trying to do is to save login details with the state info

if you realy want to help may be its better to have a look at -> http://www.michelhiemstra.nl/scripts/jsportal/index.php
mydropzCommented:
ok now i understand what you are trying to do

in that case it's better set the login name in a php session variable if you dont want to use sessions then you could echo the username in to the request function but that would mean that with firebug i would be able to let the script store the state with another username.

to put a variable in a session in php do
 session_start(); $_SESSION['login'] = 'login_name';// you could do this after succesfull login 

Open in new window

after that you can use the session variable by calling $_SESSION['login'] in php instead of trying to send the login name through javascript

SameerMirzaAuthor Commented:
exactly what I am thinking.
But you dont have to have a session started to use this php variable. there are php variables to retrieve loged in user from the client-end but problem is how would I communicate it to the back-end

unfortunatly we cant implement the login for this application
last time I did this by using the variable on the main page and used forms to communicate (hidden fields).

I dont know if I can do somthing similar in this case.
somehow I should be able to pass the username to the save.php
mydropzCommented:
if your page with the ajax.request function is a php page you could echo the variable value into the function something like this
new Ajax.Request(this.options.saveurl, { method: 'post', postBody:'login=<?php echo $login_name;?>&other=somethingelse', onComplete : function (t) {
						$('data').update(t.responseText + $('data').innerHTML);
					} });

Open in new window


i hope this makes sense to you.  it's not the safest option but it might work

if you want to use a php variable between two different php pages you need sessions and i don't see why that should be a problem

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SameerMirzaAuthor Commented:
thx
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
AJAX

From novice to tech pro — start learning today.