PDA

View Full Version : Access Form Submit result in Success Handler



rswafford
28 Jun 2007, 6:56 PM
I've got a login form working great right now. However...my login function also returns a UserID value that I would like to grab and store in a variable in my javascript. How can I do that in the function defined in the 'success' configuration option of my form submit?

fay
29 Jun 2007, 1:21 AM
Use the action parameter returned by the form's success() callback:


submitLoginDialog: function(){
var frm = new Ext.BasicForm(Ext.get("login-form"), {});
frm.submit({
url:'dologin.aspx',
success: function(form, action){
Ext.MessageBox.alert('Login Success', 'UserId = ' + action.result.UserId);
},
failure: function(form, action) {
Ext.MessageBox.alert('Login Failed', 'Please check your Username and Password.');
}
});
},

For this example, dologin.aspx just returns:


Dim UserId = 1234
Dim ResponseText = "{success:true, UserId:" & UserId & "}"
Response.Write(ResponseText)

Note: If you use FF & Firebug, put a breakpoint at the 'Login Success' line and you can evaluate everything in 'action'.

rswafford
29 Jun 2007, 5:42 AM
Thank you! That is exactly what I was looking for.

Cheers!

rswafford
29 Jun 2007, 6:29 AM
Augh...that doesn't want to work with my web service -- I'm returning XML from a standard ASP.NET Web Service. My XML Error Reader can see the node I want to get to, but I can't seem to make it show up in the action.result object.

My Error Reader:


Ext.form.XmlErrorReader = function(){
Ext.form.XmlErrorReader.superclass.constructor.call(this, {
record : 'LoginResult',
success: '@success',
id: '@userid'
}, []
);
};
Ext.extend(Ext.form.XmlErrorReader, Ext.data.XmlReader);and my loginSuccess function:


var loginSuccess = function(form, action) {
CustomerID = action.result.userid;
Ext.MessageBox.alert('CustomerID', 'CustID=' + CustomerID);
dialog.hide();
//action.result.login
}

The XML being returned looks like this:


<LoginResult>
<success>true</success>
<userid>18792.00000001</userid>
</LoginResult>

fay
29 Jun 2007, 7:02 AM
Assuming your web service sets:


Response.ContentType = "text/xml"

you can access the XML via action.response.responseXML:


var UserId = action.response.responseXML.getElementsByTagName('LoginResult')[0].lastChild.firstChild.nodeValue;

or


var UserId = action.response.responseXML.getElementsByTagName('LoginResult')[0].childNodes[1].firstChild.nodeValue;

Sorry, but I know little about XML documents; I'm sure there must be an easier way to access the userid tag than this :)

EDIT: If the web service doesn't set the ContentType to text/xml, you can parse action.response.responseText to get at userid.