PDA

View Full Version : Ext.Ajax.request and return value?



AspWhiz
6 Dec 2007, 7:48 PM
I'm using the following code to save to my db when I add a new node to my tree.



Ext.Ajax.request({
url: url,
params: { cmd: cmd, id: id, name: newValue }
});


Being an Ext JS and ajax newbie, how can I get the new id value from my db back so that I can assign the proper id to the new node in the tree?

Thanks

santosh.rajan
6 Dec 2007, 7:55 PM
var id = "....";
Ext.Ajax.request({
url: url,
params: { cmd: cmd, id: id, name: newValue },
success: myFunc,
failure: myFailureFunc
});

function myFunc(response) {
alert(id);
};

AspWhiz
6 Dec 2007, 8:01 PM
Thanks for the response (no pun intended)....but what is the best way to send the info back. I'm using PHP. I've echoed the id back and see it in the response that firebug shows. How do I access this now via the myFunc(response)? Not sure I see how your alert(id) will have the value.

santosh.rajan
6 Dec 2007, 8:24 PM
It can be either. If it is json then call
Ext.util.JSON.decode(response.responseText);
If it is xml use the xml dom functions on response.responseXML. remember to set content-type "text/xml" in php for xml.

santosh.rajan
6 Dec 2007, 8:29 PM
As a postscript. My initial "response" was because you were sending the id to the server. So the myFunc picked the function not from the response but from the same scope in which i defined "id".

AspWhiz
6 Dec 2007, 8:30 PM
responseText is what I needed. Much thanks folks!!

BTW, I looked in the docs, where can I find all the properties of response?

santosh.rajan
6 Dec 2007, 8:32 PM
response is the same XmlHttpResponse object of javascript. Look up javascript docs for that it wont be in Ext docs.

AspWhiz
6 Dec 2007, 8:35 PM
Thanks a bunch!!!

ananto
7 Dec 2007, 2:48 AM
I still don't get it. I have an xml on my php file, and I am sending request to get the data. How I put the data on my javascript variable ?



var id = 2;
Ext.Ajax.request({
url: 'xml.php',
params: { id: id },
success: myFunc,
failure: myFailureFunc
});


the xml.php will produced a record with id=2. The problem is, I want to get the value of the other field in my xml. Thanks.

santosh.rajan
7 Dec 2007, 2:55 AM
You have to use dom methods to get the value of xml fields or you can use Ext.DomQuery to access data in the xml. Read the DomQuery docs.

ananto
7 Dec 2007, 3:14 AM
Thanks for the quick reply.

How the DomQuery reads the data requested by Ext.Ajax.request ? Or we have to request with some other way ? I read the DomQuery docs, but didn't found how to request the data from the server. Could you give an example with the code please ? I really appreciate this. Thanks before.

AspWhiz
7 Dec 2007, 5:53 AM
If you just need one simple value like I did you can simply do this:

Note n is the node passed into the TreeEditor complete event. This is when I save to db the new node.


Ext.Ajax.request({
url: url,
success: function(response){
if(n.id == 'NEW_NODE'){
n.id = response.responseText;
}
},
params: { cmd: cmd, id: n.id, name: newValue }
});


on the php side all I do is:



$newID = $photos->CreateUserAlbum($user->PlayerID, $_REQUEST["name"], "", $user->PlayerID);
echo $newID;