Posted by: shijesh | March 21, 2010

AJAX – The Simplest Example

It is quite easy to implement AJAX in APEX (Thanks to Carl Backstrom for a clear and conscience document). Here is a sample AJAX code to show how easy it is to use AJAX in APEX application.

The aim of my code was to select PRODUCT_NAME from Select list and populate the textarea with PRODUCT_DESCRIPTION without reloading the page. Here is what I did.

1. Created a select list P1_PRODUCT_NAME and a textarea P1_PRODUCT_DESCRIPTION.

2. In the HTML Form Element Attribute of select list I entered

onchange="getDescription( this , 'P1_PRODUCT_DESCRIPTION')"

3. In the HTML Page Header I entered the javascript which will call the ondemand process.

<script type="text/javascript">
function getDescription( pThis , pDescription)
{
   var ajaxRequest = new htmldb_Get( 'P1_PRODUCT_DESCRIPTION' , &APP_ID. , 'APPLICATION_PROCESS=getDescription' , 0);
   ajaxRequest.add( 'P1_PRODUCT_NAME' , pThis.value);
   var ajaxResult = ajaxRequest.get(); 
}
</script>

4. Last step is to create a ondemand process. For this go to shared component -> Click on Application process and
create a new application process getDescription

declare
   prod_desc varchar2(2000);
   id number;
begin
   id := : P1_PRODUCT_NAME;
   select PRODUCT_DESCRIPTION into prod_desc from DEMO_PRODUCT_INFO
   where PRODUCT_ID = id;
   htp.prn( prod_desc );
end;

Here is the example
E X A M P L E


Responses

  1. I can’t enter your site : http://apex.oracle.com/pls/apex/f?p=12060:101:772900707162972&notification_msg=Invalid%20Login%20Credentials/E9C33CFD87E45F7910F6C7586A388183/ (Username/Password will be false)

    Can you help me ?

  2. Hi Suleyman,

    I just now checked and it is working fine..
    Password is dragon and is case sensitive..
    Try the below link
    http://apex.oracle.com/pls/apex/f?p=12060:1

    Regards,
    Shijesh

  3. Ok I got it to work, can you do an example on multiple items based on the select list?

  4. REALLY GREAT

    This was the only page that could teach me Ajax with Apex

    • id := : P1_PRODUCT_NAME;

      Here the colon should be NEXT to the name

      id := :P1_PRODUCT_NAME;

      And the script should be

      function getDescription( pThis , pDescription)
      {
      var ajaxRequest = new htmldb_Get( ‘P1_PRODUCT_DESCRIPTION’ , &APP_ID. , ‘APPLICATION_PROCESS=getDescription’ , 0);
      ajaxRequest.add( ‘P1_PRODUCT_NAME’ , $(‘#P1_PRODUCT_NAME’).children(“option”).filter(“:selected”).text());
      var ajaxResult = ajaxRequest.get();
      }

      Works on APEX 4.2.3

  5. Excellent explanation.
    I really appreciate your effort.


Leave a reply to Süleyman Cancel reply

Categories