1 2 Previous Next 18 Replies Latest reply: Sep 6, 2013 9:39 AM by backintheday RSS

    Get value of pl/sql-generated form element?

    backintheday

      Hello,

       

      I have an APEX page with a PL/SQL dynamic region.  This regions calls a PL/SQL procedure that generates html.  In this html, a form is opened with a text box (id="txtUserEnteredText") awaiting user input.  I need to know how to set the value of an APEX page item on a button click with the user-entered text. What I have tried is the following dynamic action for the button (no errors or data are returned):

       

      -- Dynamic Action

      Event: On Click

      Section Type: Button

      Button: P20_GET_TEXT

       

      -- Action

      Action: Set Value

      Set Type: JavaScript Expression

      JavaScript Expression: $x(document.getElementById('txtUserEnteredText').value);

       

      -- Affected Elements

      Selection Type: Item(s)

      Item(s): P20_DISPLAY_TEXT

       

      What I'm expecting is that the P20_DISPLAY_TEXT item will show the user-entered text after the button is clicked.  Another way to say this might be that I need is to know how to retrieve a value from an already rendered HTML element generated by the PL/SQL Web ToolKit via a PL/SQL dynamic region.  The HTML element in question is a text box that allows a user to enter text.  I need to retrieve the user's entered text in order to process that data instead of using any Web ToolKit procedures.

       

      I know this is a crazy way to do things, but I just need to know if I can do this and how.

       

      I'm using APEX 4.2.2 on Database 11.2.0.3.

       

      Thanks,

      -Seth.

        • 1. Re: Get value of pl/sql-generated form element?
          Tom Petrus

          Close, though. I'm not sure why you use $x there. Did you read the documentation on it? JavaScript APIs: $x

          Given a DOM node or string ID (pNd), this function returns a DOM node if the element is on the page, or returns false if it is not.

          So what you do is retrieve the element, take the value, and then try to retrieve a dom node with that value? Auch.

           

          Instead of that construction, use the $v notation:

          $v("txtUserEnteredText")
          //or use plain jquery
          $("#txtUserEnteredText").val()
          
          • 2. Re: Get value of pl/sql-generated form element?
            Ramesh P, Oracle APEX Developer

            Hi,

            You have text field with ID: "txtUserEnteredText"

             

            You need to set value to P20_DISPLAY_TEXT  (Not sure what type of page item)

             

            on button click : redirect to URL

             

            <script type ="text/javascript">

            function getValue(){

            var textVal = $("#txtUserEnteredText").val();

            //If it is hidden item or text field

            $("#P20_DISPLAY_TEXT").val(textVal);

            //If it is display only item then do this

            $("#P20_DISPLAY_TEXT").text(textVal);

            }

            </script>

             

            You can do like this or you can put the following code in dynamic action javascript on click

             

            var textVal = $("#txtUserEnteredText").val();

            //If it is hidden item or text field

            $("#P20_DISPLAY_TEXT").val(textVal);

            //If it is display only item then do this

            $("#P20_DISPLAY_TEXT").text(textVal);

            • 3. Re: Get value of pl/sql-generated form element?
              backintheday

              Tom,

               

              To test, I am trying a simple button click dynamic action that executes javascript.  Here is what I did with your examples::

               

              alert($v("txtUserEnteredText"));

               

              That returns a blank alert window.

               

              alert($("#txtUserEnteredText").val());

               

              That returns undefined in the alert window.

               

              So neither one of them worked.  When I use the $x option, I get an [object] returned in the alert window which leads me to believe the javascript can see the element.

               

              alert($x(document.getElementById('txtUserEnteredText')));

               

              Also, the element from which I am trying to get a value is not an APEX page item.  It is an already rendered element in the HTML generated by PL/SQL code (see below, and note the hardcoded value for testing purposes which is what I'm expecting to have returned):

               

              htp.tableData(htf.formText('txtUserEnteredText','7','7','1234',cattributes=>'class="udata"'),'center','','','','',cattributes => 'class="udata" width="80"');

               

              Thanks,

              -Seth.

              • 4. Re: Get value of pl/sql-generated form element?
                backintheday

                Ramesh,

                 

                When I try the first method, I simply get an 400 - Bad Request error ("The request path contains illegal characters") because the whole script becomes a URL.

                 

                The second method using a dynamic action returns the following into the page item (P20_DISPLAY_TEXT of type text):

                 

                [object Object]

                 

                Or if I set the page item P20_DISPLAY_TEXT to a display only field (with the appropriate javascript line), I get nothing returned.

                 

                -Seth.

                • 5. Re: Get value of pl/sql-generated form element?
                  Ramesh P, Oracle APEX Developer

                  Hi,

                  What type of page item is P20_DISPLAY_TEXT

                  • 6. Re: Get value of pl/sql-generated form element?
                    Tom Petrus

                    I'm not sure what is going on then. Since I see you are writing table data, is what you have some sort of tabular form setup?

                    When you run

                    document.getElementById("txtUserEnteredText")
                    

                    from the console, then what do you get? Because checking htf.formText shows that you the first attribute is not an id but the name attribute. If you do retrieve something by using it as an id it won't be the input item. And since this may be a tabular form it wouldn't be apt too. Either way, running document.getElementById and not providing a valid id would instantly throw an error. The reason it does not in your case, well, to be determined.

                    Try running

                    $("input[name=txtUserEnteredText]")
                    

                    from the console, see what that provides you.

                     

                    At least I'm confident that your $x setup is not suitable. It is used to retrieve a dom node. Why would you need to wrap the retrieved value from the element then? No matter that you get some object back from it, it doesn't make sense.

                    • 7. Re: Get value of pl/sql-generated form element?
                      backintheday

                      Ramesh,

                       

                      I went back and forth between text and display only using the two different lines you suggested.

                       

                      -Seth.

                      • 8. Re: Get value of pl/sql-generated form element?
                        backintheday

                        Tom,

                         

                        Where is the "console"?  I'm not sure what you mean by that.

                         

                        -Seth.

                        • 9. Re: Get value of pl/sql-generated form element?
                          backintheday

                          Tom,

                           

                          I found a console in the developer tools for my browser.  I'm assuming that's what you meant.  Here is what was was returned;

                           

                          > $("input[name=txtUserEnteredText]")

                          [<input type="text" name="txtUserEnteredText" size="7" maxlength="7" value="2018" class="udata">]

                           

                          -Seth.

                          • 10. Re: Get value of pl/sql-generated form element?
                            backintheday

                            Thank you, Tom and Ramesh, for your help.  The syntax that finally worked in my Dynamic Action was this:

                             

                            $("input[name=txtSessionStart]").val()

                             

                            This was based upon Tom's last suggestion to run the following in the console:

                             

                            $("input[name=txtSessionStart]")

                             

                            I added the .val() to get the value.

                             

                            Thanks!

                            -Seth.

                            • 11. Re: Get value of pl/sql-generated form element?
                              backintheday

                              Here is what I ultimately did to get data into the database...

                               

                              1.  At the page level, I created the following function to set the value of the hidden page item:

                              function setItemValue(pRequest) {

                                var itemVal = $("input[name=txtUserEnteredText]").val();

                                $("#P20_DISPLAY_TEXT").val(itemVal);

                                doSubmit(pRequest);

                              }

                               

                              2.  Then I created a Region Item Button named SUBMIT (as Redirect to URL) that called the function:

                              javascript:setItemValue('SUBMIT');

                               

                              3.  Lastly, I created a page process (as After Submit... and when button pressed) calling a package procedure to do the actual update:

                              pkg_name.proc_name(:P20_DISPLAY_TEXT);

                               

                              Thanks,

                              -Seth.

                               

                              SPECIAL NOTE: I was never able to get this to work in IE - only in Chrome, Firefox and Safari - because of a "Page protection validation" error.

                               

                              Message was edited by: backintheday "I added the special note about the browser difference."

                              • 12. Re: Get value of pl/sql-generated form element?
                                Tom Petrus

                                Seth,

                                 

                                I'm not sure why you moved away from a dynamic action for this, you can define all these steps in one. A set value with type javascript expression, affect the item, and another true action to submit the page. You can set the region button to "defined by dynamic action" instead of putting javascript in the href tag.

                                Not sure why you get the error in ie only, there is nothing in this code that could cause this. If P20_DISPLAY_TEXT were a hidden item and it was set to protect the value, then you'd get an error in all browsers. Are you sure it's this code causing this? Maybe moving it to a dynamic action may solve it.

                                • 13. Re: Get value of pl/sql-generated form element?
                                  backintheday

                                  Tom,

                                   

                                  You make a good point; but at the time you gave me the correct code to get the value, I had been trying the javascript at the page level.  Therefore, I went ahead and used the code there to get it working.  I like your idea of putting it all into a dynamic action and will give that a try.  P20_DISPLAY_TEXT is indeed a hidden item with its protection set to No, so that's a good observation there.  I was getting the IE error when trying javascript in dynamic actions earlier but perhaps incorrect code was causing it.  I'll report back what I find.

                                   

                                  Thanks,

                                  -Seth.

                                  • 14. Re: Get value of pl/sql-generated form element?
                                    backintheday

                                    Tom,

                                     

                                    Your suggestion to use a Dynamic Action does indeed work.  Unfortunately, IE still suffers from the "Page protection violation" error.

                                     

                                    For the dynamic action on the button click to Set Value, I have a True action that contains the following javascript line that affects hidden item P20_DISPLAY_TEXT:

                                     

                                    $("input[name=txtUserEnteredText]").val();

                                     

                                    Then another True Action within the same dynamic action that simply submits the page.  The page process I had already created (as noted earlier) then affects the database.

                                     

                                    Thanks,

                                    -Seth.

                                    1 2 Previous Next