Introduction
Adding values from two form controls into a table in Oracle APEX is a common requirement when capturing user input across multiple fields. Whether you're entering product details, capturing a name and email, or logging coordinates, the process involves creating input items on a page and using a submit process to store those values in the database. With Oracle APEX, this can be achieved using either declarative features or a custom PL/SQL block, making it simple to integrate into both basic and complex applications.
To add values from two controls into a table in Oracle APEX, you use page items (form fields), a submit button, and a process that takes the values from those items and inserts them into a target table. This is a common scenario for capturing basic data like a name and email, or a code and description.
Here’s a detailed step-by-step process:
Step 1: Create the Table (if it doesn’t exist)
First, define the table that will store the values. For example:
CREATE TABLE CONTACT_LOG (
ID NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
FULL_NAME VARCHAR2(100),
EMAIL VARCHAR2(100),
CREATED_AT DATE DEFAULT SYSDATE
);
Step 2: Create a Page in Oracle APEX
-
Open your application in App Builder.
-
Click Create > Page.
-
Choose Blank Page.
Name the page (e.g., “Add Contact Info”).
Step 3: Add Form Controls (Page Items)
In the page designer:
-
Under a region (e.g., "Contact Form"), add two page items:
-
P1_FULL_NAME
– Type: Text Field – Label: Full Name -
P1_EMAIL
– Type: Text Field – Label: Email
-
These will be the input controls.
Step 4: Add a Button to Submit
-
Under the same region or a new one, add a button:
-
Name:
SAVE
-
Label:
Save
Action: Submit Page
-
Step 5: Add a PL/SQL Process to Insert the Values
-
In the Processing section, click “+” to add a new Process.
-
Name:
Insert Contact Log
-
Type: PL/SQL Code
-
Point: After Submit
-
When Button Pressed:
SAVE
PL/SQL Code:
INSERT INTO CONTACT_LOG (FULL_NAME, EMAIL)
VALUES (:P1_FULL_NAME, :P1_EMAIL);
Step 6: Add a Success Message (Optional)
In the Messages section of the page:
Success Message:
Contact saved successfully.
Step 7: Run and Test
-
Run the application.
-
Enter values in the Full Name and Email fields.
-
Click Save.
The record is inserted into the
CONTACT_LOG
table.
Optional: Clear Form Fields After Insert
If you want to clear the form after submission:
-
Add another Process after the insert (or include it in the same one).
-
Example:
:P1_FULL_NAME := NULL;
:P1_EMAIL := NULL;
This resets the form for the next entry.
Adding values from two controls into a table in Oracle APEX is quick and efficient using declarative form items and a simple PL/SQL process. This method provides flexibility to collect and store input from users in a secure and controlled way. Whether you're building a contact form, data logger, or simple data capture tool, this approach gives you the core structure to expand into more complex applications.
Adding Values from Two Textbox Items into a Database Table in Oracle APEX
Oracle APEX allows inserting values from multiple form fields into a database table using PL/SQL processes, dynamic actions, and form submissions. This tutorial covers different ways to insert values from two textbox items into a table.
Step 1: Create a Form with Two Textbox Items
Open your Oracle APEX application.
Click Create Page > Form > Form on a Table with Report.
Choose the table where the values will be inserted (e.g., EMPLOYEE_DETAILS).
In Page Designer, create two text fields:
P1_EMP_NAME (for Employee Name)
P1_EMP_EMAIL (for Employee Email)
Add a Submit or Save button (P1_SAVE_BUTTON).
Step 2: Create a PL/SQL Process to Insert Data
In Page Designer, go to the Processing section.
Click Create Process, select PL/SQL Code, and enter the following:
BEGIN
INSERT INTO employee_details (emp_name, emp_email)
VALUES (:P1_EMP_NAME, :P1_EMP_EMAIL);
COMMIT;
END;
Set Execution Point to After Submit and When Button Pressed to P1_SAVE_BUTTON.
Click Save and Run the page.
When users enter values in the textboxes and click Save, the data will be inserted into EMPLOYEE_DETAILS.
Step 3: Confirm Success with a Message
To show a success message after inserting the record:
Open Page Designer, go to Messages, and add the following code inside the PL/SQL process:
apex_util.set_session_state('P1_SUCCESS_MSG', 'Record successfully added!');
Create a new static region and add a Display Item (P1_SUCCESS_MSG) to show the message.
Set Source to PL/SQL Expression:
:P1_SUCCESS_MSG
Now, after submitting, users will see a confirmation message.
Alternative: Insert Data Using AJAX (Without Page Refresh)
If you want to insert data without refreshing the page, use AJAX and Dynamic Actions.
Step 1: Create an AJAX PL/SQL Process
Go to Shared Components > Application Processes.
Click Create Process, name it INSERT_EMPLOYEE_AJAX, and enter the following PL/SQL:
BEGIN
INSERT INTO employee_details (emp_name, emp_email)
VALUES (:P1_EMP_NAME, :P1_EMP_EMAIL);
COMMIT;
END;
Set Process Type to Ajax Callback.
Step 2: Create a Dynamic Action
In Page Designer, select the Save Button (P1_SAVE_BUTTON).
Click Create Dynamic Action and set:
Event: Click
Action: Execute JavaScript
Add this JavaScript code:
apex.server.process("INSERT_EMPLOYEE_AJAX", {
pageItems: "#P1_EMP_NAME, #P1_EMP_EMAIL"
}, {
success: function(data) {
alert("Record successfully added!");
apex.item("P1_EMP_NAME").setValue("");
apex.item("P1_EMP_EMAIL").setValue("");
},
error: function(request) {
alert("Error inserting record.");
}
});
This method inserts values without refreshing the page and clears the textboxes after insertion.
Best Practices
Validate Inputs: Ensure users enter valid data before inserting records.
Use Dynamic Actions for Better User Experience: Avoid full-page reloads by using AJAX.
Confirm Successful Insertions: Show success messages after inserting values.
Use Form Submissions for Simple Implementations: When AJAX is not required, a simple PL/SQL process on Submit works well.
Oracle APEX provides multiple ways to insert values from two textboxes into a table. You can use PL/SQL processes for form submissions or AJAX for real-time updates without refreshing the page. The best approach depends on your application's requirements.
EXAMPLE:
Assuming a page with 2 textboxes and a button. We want to take the two values from the controls and add them into the database as a new entry.
Step 1 – Add two textboxes to page
Step 2 – Add a button with a Dynamic Action with two true branches
Step 3 – Set the following code in the first true branch
The code is as follows:
BEGIN
INSERT INTO TEST_A (VALUEA1, VALUEA2)
VALUES (:P49_TESTA_VALUEA1_VALUE,:P49_TESTA_VALUEA2_VALUE);
END;
Step 4 – add a report at the bottom of the page with the following settings.
Step 5 – In the button’s second True branch set as follows.
You should now see something like this
Conclusion
Inserting values from two controls into a table in Oracle APEX is a foundational task that reinforces the platform’s strength in rapid form development. With a few clicks or lines of code, developers can bind input fields to database columns and ensure accurate data capture. This process supports flexible UI design while maintaining data consistency, forming the backbone of many real-world APEX applications.
No comments:
Post a Comment