AJForm Documentation - AJForm Home

STEP 3 - Create AJForm callback function

The callback function is the function that will be called after a form is submitted and after the data is sent to the server. The callback function should have 3 arguments. Here is an example callback function:

function exampleCallback( AJFormStatusCode, UserData, ResponseObject ) {
  if( AJFormStatusCode == AJForm.STATUS['SUCCESS'] ) {
   alert( "Data sent successfully." );
  }
}

Or, if your form contains file uploads and the AJForm server scripts are not supported on your system, an example callback function would look like this:

function exampleCallback( AJFormStatusCode, UserData ) {
  if( AJFormStatusCode == AJForm.STATUS['SUCCESS_NORESPONSE'] ) {
   alert( "Data sent successfully, but no response from the server is available." );
  }
}

Here are explanations for the 3 possible callback arguments:

  1. AJFormStatusCode

    This is a positive integer which can help to determine the outcome of the AJForm request. Possible values are:

    Variable Name Status Code Description
    AJForm.STATUS['SUCCESS'] 0 The form was submitted through AJFORM successfully.
    AJForm.STATUS['SUCCESS_NORESPONSE'] 1 The form was submitted through AJFORM successfully, however, the form contained a file upload AND the AJForm server scripts were not present or not supported. When this is the case, no server response data will be available.
    AJForm.STATUS['HTTP_OBJECT_FAILED'] 2 The HTTP Request Object could not be intitiated. This will occur primarely in older browsers that do not support this feature.
    AJForm.STATUS['SERVER_ERROR'] 3 The server returned an HTTP error.
    AJForm.STATUS['UNKNOWN_ERROR'] 4 HTTP status not available. Hopefully, you won't run into this error.
  2. UserData

    This is the arbitrary user data (if any at all) which was passed to the AJForm callback function in the AJForm form initilization. You may remember from the previous step, that your form initialization may have looked something like this:

    <form action="example.php" onsubmit="ajform:exampleCallback( 'Hello World' );">

    In this example, UserData would be Hello World. However, if you pass MULTIPLE arguments in the form initialization such as:

    <form action="example.php" onsubmit="ajform:exampleCallback( 'Hello World', 888, 'Foo' );">

    In this case, UserData would be an array:

    • UserData[0] : 'Hello World'
    • UserData[1] : 888
    • UserData[2] : 'Foo'
  3. ResponseObject

    This argument is not available if the initialized form contains a file upload AND the AjForm server scripts are not supported.

    This is an object containing various properties which can give detailed information about the server response. The properties are as follows:

    Property Namee Data Type Description Example
    ResponseObject.responseText String This contains the response body from the server.

    alert(ResponseObject.responseText);

    ResponseObject.status Integer This contains the HTTP status code form the server.

    alert(ResponseObject.status);

    ResponseObject.statusText String This contains the HTTP status message the server.

    alert(ResponseObject.statusText);

    ResponseObject.headers Array of Key/Values This contains a hash array of HTTP response headers.

    alert(ResponseObject.headers['Content-Type']);

    ResponseObject.responseXML DOM XML Object This will only work as expected if the server returns valid XML.

    alert(ResponseObject.responseXML.documentElement.nodeName);