Saturday, 30 May 2009

Execute Javascript through PL/SQL

Sometimes, like in this case, where I needed to set the focus on load to a specific line in a table when inserting new rows, you need to execute javascript within your PL/SQL block. In this example in my Demo Application you will find an explanation of the code you need for that.




15 comments:

John Scott said...

Hi Denes,

Just to be pedantic, the Javascript is not actually being executed by the PL/SQL, the PL/SQL is purely emitting the Javascript code (essentially text at this point) to the browser. The browser then interprets this code during the page rendering phase (rendering in the browser I mean).

It might seem like a subtle difference, but I just wanted to point out that the Javascript is not running at the same time the PL/SQL code is running in this case.

John.

Denes Kubicek said...

Sure, but this is how this question will be asked and understood by the most. This is why I picked that title. Whoever reads the code will soon notice that there is no execution of the javascript through PL/SQL. PL/SQL is used to put the javascript code together and send it to the browser.

Chris said...

By the way, it's not that hard to really "execute javascript through PL/SQL". About a year ago, I loaded rhino into a 10.2.0.4 server and played with it a little bit. It never had any use except to prove to myself that I'm still a nerd ;-).

Anonymous said...

Hello Denes,
I understood what u have specified, but I want to do some other thing, I want to run a sql query while checking a condition in javascript like this
htp.p('if(v == true){');
update aggrement set checked='y' where agg_no = :P2_AGG_NO;
htp.p('}else{');
update aggrement set checked='n' where agg_no = :P2_AGG_NO;
htp.p('}');
but it is not working. This always insert 'n' in "checked" column.
Please Help me out on this.
you can send me a mail on this on
sheikh.tauceef@gmail.com
Thanks.

Denes Kubicek said...

I don't think you need javascript for that since this can be done in the query itself. See this example from Patrick Wolf:

http://www.inside-oracle-apex.com/checkboxes-in-tabular-forms-the-easy-way/

Denes Kubicek

Anonymous said...

actually i m doing this in a query only see this whole logic

Declare
Cursor agg is
select Agg_no,client.Client_name clientn,End_date from AGGREMENT,client WHERE client.client_id = aggrement.client_id;
Begin
for agrmt in agg loop
:P2_END_DATE := agrmt.End_date;
:P2_AGG_NO := agrmt.Agg_no;
:P2_CLIENT_NAME := agrmt.clientn;
if to_date(agrmt.End_date,'dd-mon-rr') <= to_date(SYSDATE,'dd-mon-rr') THEN
htp.p('open script);
htp.p('pop('''
|| :P2_AGG_NO
|| ''','''
|| :P2_END_DATE
|| ''','''
|| :P2_CLIENT_NAME
|| ''');');
htp.p('if(v == true){');
update aggrement set checked='y' where agg_no = :P2_AGG_NO;
htp.p('}else{');
update aggrement set checked='n' where agg_no = :P2_AGG_NO;
htp.p('}close script');
end if;
end loop;
End;

This is my whole logic where I am calling a javascript function from select query from that function I m getting the value of 'v' which is a boolean variable and after checking this variable's value I want to update my table accordingly.

this is the Pop function:

var v;
function pop(ag_no,end_dt,clnt_nm)
{
v = confirm("Aggrement: " + ag_no + " with Client: " + clnt_nm + " is going to end on " + end_dt + " Date.");
}


For this either I need to run the update table qurery under javascript or I can reffer the javascript variable 'v' into my pl/sql function.

Anonymous said...

Sir please reply soon, Because I need it in my project on which I am working.

Waiting for your reply..

Denes Kubicek said...

I am sorry but I still do not understand your requirement. I see your statement but I don't see what exactly you want to do with it.

Denes Kubicek

Anonymous said...

Actually I have a Confirm Box,
which will get popped up when the aggrement end date is less than or equal to today's date, through that confirm box I am getting the value in variable "v" as true or false(after user pressed OK or CANCEL). After checking this value I want to update my "Aggrement" table.

So my problem is that the variable "v" is a Javascript variable and I want to update my table after checking this variable's value.

If u can tell me how can I access the variable "v" in PL/SQL function? If I can't do this than tell me can I run my update query in javascript after checking the value of "v".

Hope this explanation helps u understand my problem.

Thanks for helping me out. Please see if u can reply soon.

Anonymous said...

If you want you can check this in my workspace

Login Information:
workspace:tauceef
username:denes
password:denes

Application: Sales Management-2098
Page:Aggrement(page no.2)

Anonymous said...

Sorry I forgot to mention
You can login on
apex.oracle.com/pls/apex using above login information

Denes Kubicek said...

I will have a look at it when I have time. I am currently bussy with my daily stuff.

Denes Kubicek

Nipun said...

hi denes,

how it possible to show a "user choice confirmation message" during a pl/sql process..?

Arnaud said...

Hi

I have the same question that Nipun

I try to execute sql after a javascript confirm


htp.p('if (confirm("Do you want to delete your record?")) {' );
delete emp where emp_id = :P1_EMP_ID; );
commit;
htp.p('}');


But the delete will be execute at each time.

Arnaud said...

I find something like that

If you cancel return to your page else delete the row

htp.p('if (!confirm("Do you want to delete your record?")) {' );
htp.p('redirect("f?p=&APP_ID.:1:&APP_SESSION.")');
htp.p('}');

htp.p('alert("delete in progress : ");' );

delete emp where emp_id = :P1_EMP_ID;
commit;