PDA

View Full Version : Variables through Ajax



kuzi
18 Aug 2008, 8:25 AM
Hi,
I'm trying to get response from database through Ext.Ajax.request object. I call ajax on the start of a new window (this window is opened after clicking a row in the grid) to get first name and last name of the person (from database). The variables (defined as global) are set in success function of ajax request. They should store data I need (first name and last name). I don't use any store like JSON (it's a simple text - the format is: FirstName, LastName as a response passed to ajax). When I try to get value of FirstnameField, it is undefined. Why? It's a problem of scope and async of Ajax I suppose. But is there other solution to achieve it?
My code:


var FirstnameField;
var LastnameField;

Ext.onReady(function(){

Ext.getUrlParam = function(param) {
var params = Ext.urlDecode(location.search.substring(1));
return param ? params[param] : params;
};

var idpatient = Ext.getUrlParam('IDpatient');

Ext.Ajax.request({
waitMsg: 'Wait...',
url: 'db.php',
params: {
task: "LOADNAME",
IDpatient: idpatient
},
success: function(response){
var result = response.responseText.split(",");
FirstnameField = result[0];
LastnameField = result[1];
},
failure: function(response){
var result = eval(response.responseText);
Ext.MessageBox.alert('Failure','Mission failed');
}
});
...
alert(FirstnameField);
...
});


Thanks for help in advance!

Chris

euchiyama
18 Aug 2008, 8:35 AM
Perhaps you're not getting Success??

Debug it in Firefox and see.

I thought you had to return Success or Failure along with your server response string...

devnull
18 Aug 2008, 8:42 AM
the "a" in ajax stands for asynchronous, you need to process the result within the callback. in this case the code is getting to the alert statement before the ajax call has returned.

kuzi
18 Aug 2008, 8:45 AM
I'm getting success, because I have access to this variables inside success function... (through alert call). Server return also proper response (I got "Firstname,Lastname" through console in Firebug).

kuzi
18 Aug 2008, 8:50 AM
So I need requestcomplete event on Ajax request? How can I fill the variables with values from database through ajax?

devnull
18 Aug 2008, 8:52 AM
Whatever code you have that makes use of those variables either needs to be within the success function or be called from within the success function.

kuzi
18 Aug 2008, 9:09 AM
How can I pass the variables from ajax (inside success function) to another window (FormPanel or TabPanel) without using stores (JSON)?

devnull
18 Aug 2008, 9:30 AM
Your best bet is probably to create this other window within the success function of the ajax call.

kuzi
18 Aug 2008, 9:43 AM
Thanks, devnull :)