Link to home
Start Free TrialLog in
Avatar of SameerMirza
SameerMirzaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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

Avatar of mydropz
mydropz

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

Avatar of SameerMirza

ASKER

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

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
/ 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;
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

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
parameterz dont work when you have set the postbody..
you actually confirmed it in the previous post and I experienced it :)
ok but the retreiving of the values made sense ???
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
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
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?
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
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
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

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
ASKER CERTIFIED SOLUTION
Avatar of mydropz
mydropz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thx