Search code examples
parameter-passingoracle-apexdashboard

Oracle Apex : Accessing params passed through URL


I am passing parameters through the URL and the page is getting correctly redirected. How do I access the parameters on the next (redirected) page that were passed (from previous page) and available in the URL? Can anyone please tell me how to do this?

Or is there any other way to pass parameters between pages?

Is there any option available similar to broadcast event from Angular?

I do not want to use Global Page option.


Solution

  • Is there any option available similar to broadcast event from Angular?

    Not really. Though you could do this with APEX, it's probably overkill. Angular is a SPA framework that does client-side rendering whereas APEX does mostly server-side rendering. You'll have to rethink some things if you're coming from the Angular world.

    How do I access the parameters on the next (redirected) page that were passed (from previous page) and available in the URL?

    The first question is: are you trying to access the values using JavaScript or SQL & PL/SQL? I'll answer both, but first I'll take a step back to explain session state in APEX. The following is oversimplified but should help to give you an idea of what's happening behind the scenes.

    Keep in mind that the APEX engine is a schema inside Oracle Database which contains many tables (mostly to store application metadata), packages (for business logic and HTML generation), and other database objects. Among those tables are a few for storing "session state". Think of the two primary tables as SESSIONS and SESSION_DATA. SESSIONS would have a unique identifier for the session, the username, etc. SESSION_DATA would have a SESSION column that points back to the SESSIONS table, an ITEM_NAME column, and an ITEM_VALUE column.

    Imagine you go to the following URL: https://yourdomain.com/apex/f?p=101:1:12690960447054

    The APEX engine will pick up on the "p" parameter of the URL and parse it into up to 9 values:

    1. App
    2. Page
    3. Session
    4. Request
    5. Debug
    6. Clear Cache
    7. Item Names
    8. Item Values
    9. Printer Friendly

    In the URL above, only three of those values are specified, so the APEX engine generates the HTML for page 1 in app 101 for session 12690960447054.

    But what if you use the following URL: https://yourdomain.com/apex/f?p=101:1:12690960447054::::P1_ITEM_NAME:Dan

    In this case, the APEX engine will update session state in the SESSION_DATA table before generating the HTML. It will set the ITEM_VALUE column to 'Dan' where the SESSION is 12690960447054 and the ITEM_NAME is 'P1_ITEM_NAME'.

    So that's what's technically happening when you set session state via a URL. When you're ready to learn even more about session state, check out this old but still relevant post from Anton Nielsen. Now let's move on to your question about accessing session state...

    SQL & PL/SQL

    Once APEX has set session state, it proceeds to generate the HTML. Let's say you had a report region on the page with the following query:

    select col1, col2, col3
    from some_table
    where col4 = :P1_ITEM_NAME
    

    The APEX engine will identify the bind variable in the query and provide the correct value from the SESSION_DATA table as per the session. Frankly, you can access the value of items from any page, as well as application items, because we are server-side at this point and it really makes no difference. Having said that, the best practice is to reference only page items from the current page.

    See "Referencing Items Values" for more ways to access session state server-side: https://docs.oracle.com/en/database/oracle/application-express/19.1/htmdb/understanding-page-level-items.html#GUID-207E66F9-C8A6-4FD1-B2F4-12D4BB302D31

    JavaScript

    Once the HTML is generated, it's sent to the browser to be rendered. For JavaScript to access values from session state without using Ajax (which you should mostly avoid), the values must have made it to the DOM. If P1_ITEM_NAME was rendered on the page, then its value should be 'Dan'. You can get this value using raw JavaScript:

    document.getElementById('P1_ITEM_NAME').value
    

    or jQuery:

    $('#P1_ITEM_NAME').val()
    

    However, it's best to use APEX specific JavaScript APIs that understand how APEX works with things like checkboxes and radio groups. For example, you could use:

    $v('P1_ITEM_NAME')
    

    Or you could use:

    apex.item('P1_ITEM_NAME').getValue()
    

    Keep in mind that if the item didn't make it to the DOM, say because it belonged to another page or because of a condition, then you will not be able to access it in JavaScript.