This discussion is archived
8 Replies Latest reply: Sep 13, 2013 9:22 AM by Siberia RSS

insert a oracle table from jquery/ajax

Siberia Newbie
Currently Being Moderated

Hi.

i am new on apex and will like to know if can help me with my question.

i am calling an on demmand process from jquery javascript to insert some data on oracle apex_collection (i do delete and insert on the apex collection and works fine) , but i need to insert an oracle database table directly from jquery (through an on demmand process or any other way).

 

actually, i have an html page with an anonymus pl/sql which shows a list of items where user enter some values; using javascript, i call an on demmand - javascript process that will do the insert on an oracle table, but nothing happens. no data get inserted to my table

actually i am calling the on demmand process using this on javascript, and works nice, but only on apex_collection , not on oracle database table.

 

                          var ajaxRequest = new htmldb_Get(null,&APP_ID.,"APPLICATION_PROCESS=ADD_PRODUCTS",&APP_PAGE_ID.);

                          ajaxRequest.add('P1_PRODUCT_ID',html_GetElement('P1_PRODUCT_ID').value);

                          ajaxRequest.add('P1_QTY',html_GetElement('P1_QTY').value);

                          ajaxResult = ajaxRequest.get();

 

it is importante to mention that i am NOT SUBMITTING or refreshing the page at any time, because it will make it too slow, I think it has something to do with the problem but not sure.

 

thanks in advance.

Anthony.

  • 1. Re: insert a oracle table from jquery/ajax
    scott.wesley Guru
    Currently Being Moderated

    Can you also supply the PL/SQL process you're calling?

     

    If you're version 4+, you could just use dynamic actions.

  • 2. Re: insert a oracle table from jquery/ajax
    Siberia Newbie
    Currently Being Moderated

    Hi Scott. thanks for your interest.

    ok, i am using Application Express 4.2.2.00.11 (Oracle Database SE 11gR2)

     

    i have created a HTML region Source with this code to call the Ajax Process :

     

     

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

    <title>Totalizar</title>

    <script type="text/javascript">

    $(function()

    {

     

     

     

            // Save the Order

            $("#Save").click

               (

                function()

                 {

                     var ajaxRequestd = new htmldb_Get(null,&APP_ID.,"APPLICATION_PROCESS=SAVE",&APP_PAGE_ID.);

                     ajaxRequestd.add('P1_CODIGO_CLIENTE',html_GetElement('P1_CODIGO_CLIENTE').value);

                     ajaxRequestd.add('P1_TIPO_CLIENTE',html_GetElement('P1_TIPO_CLIENTE').value);

                     ajaxResult = ajaxRequestd.get();

                     alert('Order Placed !');

                  }

               );

     

     

    }

    );

    </script>

    </head>

    <body>

    <div id="totals"></div>

     

    <p align="center" style="clear: both;">

    <button type="button" style="font-weight: bold;background-color:lightgray;margin-left:auto;margin-right:auto;display:block;margin-top:0%;margin-bottom:0%" id="Save">Place Order</button>

    </p>

    </body>

    </html>

     

    the Message 'Order Placed !' is displayed on the page, so it tells me that the javascript is executing (i will like to display some message or alert inside the SAVE ajax process, to make sure it executes and check values at that moment, but don't know how)

     

    and here is my "SAVE" on Demand process, with this settings:

          - Process Point - Run this process when request by ajax

          - Run Process - One per Page visit

     

    whit this source code on it:

     

       -- Insert into table

        insert into cab_orders(customer_id, order_total, order_timestamp, user_name)

           values(:P1_CODIGO_CLIENTE, null, systimestamp, upper(:APP_USER));

        commit;

     

     

    Remember that at this point the page has not been refresehed or submitted, maybe tis where the problem come from.

    Thanks in advance for any tip !

  • 3. Re: insert a oracle table from jquery/ajax
    scott.wesley Guru
    Currently Being Moderated

    While I don't see anything untoward, since you're on 4.2 you might as well define this declaratively using a dynamic action.

     

    Your button's action would be defined by dynamic action

    The dynamic action would be on click of your button, executing pl/sql

     

    All you need is

     

    insert into cab_orders(customer_id, order_total, order_timestamp, user_name)

           values(:P1_CODIGO_CLIENTE, null, systimestamp, upper(:APP_USER));

    And page item to submit would be P1_CODIGO_CLIENTE

  • 4. Re: insert a oracle table from jquery/ajax
    Siberia Newbie
    Currently Being Moderated

    ok Scott. but one question.

    if i place a button on my page, when i push on it, my page will be reloaded again ?

    bacause this is exactly what i don't want, because this app is running on mobile devices on street, so reload the page takes too long, because i show about 100 products on page.

    thanks

  • 5. Re: insert a oracle table from jquery/ajax
    scott.wesley Guru
    Currently Being Moderated

    Not if you set your button action as "defined by dynamic action"

     

    I use a similar configuration all the time to refresh only certain regions on the page.

     

    Dynamic actions rock.

  • 6. Re: insert a oracle table from jquery/ajax
    Tom Petrus Expert
    Currently Being Moderated

    This is so bad. You created a region type HTML and put that HTML in its source? Essentially embedding an HTML element within another? You shouldn't (usually) embed your javascript in an HTML, and if you do then all you require are script tags, not a full page body.

    You can define javascript functions in the properties of the page, which has a dedicated section to javascript function declarations, onload code section and file urls. You should put your function there.

    And while you're doing that you should consider that you are using deprecated javascript code. html_GetElement for example, htmldb_Get is another. You should consider replacing them with JavaScript APIs: $v and JavaScript APIs: apex.server.process respectively.

     

    Similarly, why do you define your own button? Apex allows you to define both region and page item buttons. You're obscuring code for yourself (and any other person that will ever have to work on this) when there is absolutely no need to do so.

  • 7. Re: insert a oracle table from jquery/ajax
    Siberia Newbie
    Currently Being Moderated

    Excellent !. i didn't know this.

    I reviewed the SAVE process and the problem is that the table inserted in the process, has a trigger and the trigger fails.

    The problem is that the process does not indicate that there is an error, so i didn't know. Do you know how to get errors on process?

  • 8. Re: insert a oracle table from jquery/ajax
    Siberia Newbie
    Currently Being Moderated

    jejejeje. Hi Tom.

    well, as i said i am new on apex, so i hope no one uses this scripts as reference.

    i created an html page, then i created a sub region where i placed the source.

    i will change the deprecated code for the new one. thanks for your comments .

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points