Skip to Main Content
  • Questions
  • How to use checkbox on web form so only checked records are updated.

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Richard.

Asked: February 14, 2004 - 10:20 pm UTC

Last updated: June 14, 2005 - 4:25 pm UTC

Version: 9.2.0

Viewed 1000+ times

You Asked

Hi Tom,

I'm trying to learn how to use the checkbox on a web form.
What I wan't the package to do is update the record if the checkbox box was checked.
The user may click on 1 or more checkboxes. Such as checkbox 2, 5, 7, etc...these
records will be updated.

If the user does not click on a checkbox
then no records will be updated when the formsubmit is clicked.

I can not get the owa_util.ident_arr array(s) to coordinate. For example, if I
click on the second checkbox, I update the wrong record...

Thanks,
Richard

--here is the code using emp table.

CREATE OR REPLACE package demoUpdate_Checkbox6
as


PROCEDURE sample2_form_emp ;

procedure do_something( p_old_cols in owa_util.ident_arr,
p_old_ename in owa_util.ident_arr,
p_new_ename in owa_util.ident_arr,
p_old_job in owa_util.ident_arr,
p_new_job in owa_util.ident_arr);
end;
/
CREATE OR REPLACE package body demoUpdate_Checkbox6
as

PROCEDURE sample2_form_emp
IS

BEGIN
htp.htmlopen;
htp.bodyopen;
htp.paragraph;
htp.formopen('demoUpdate_Checkbox6.do_something');

htp.tableOpen;
htp.tableRowOpen; -- <TR>
htp.tableHeader ('Check Emp no to Update');
htp.tableHeader ('Emp name');
htp.tableHeader ('Job');
htp.tableRowClose; -- </TR>

-- do we need to initialize the arrays??
htp.formHidden( 'p_old_cols', 'x');
htp.formHidden( 'p_old_ename', 'x');
htp.formHidden( 'p_new_ename', 'x');
htp.formHidden( 'p_old_job', 'x');
htp.formHidden( 'p_new_job', 'x');

-- Is this te correct way to get the records I want?
for x in ( select '</td><td><input type="checkbox" value="'||to_char(empno)||'"name=p_old_cols>'||to_char(empno) empno,
htf.escape_sc(ename) ename,
htf.escape_sc(job) job
from emp
order by empno asc ) loop

htp.p(to_char(x.empno)) ;

htp.p( '</td><td>' );
htp.formHidden( 'p_old_ename', x.ename);
htp.formText( 'p_new_ename', cvalue => x.ename );

htp.p( '</td><td>' );
htp.formHidden( 'p_old_job', x.job);
htp.formText( 'p_new_job', cvalue => x.job );

htp.p( '</td></tr>' );

end loop;

htp.formsubmit(NULL,'Go');
htp.formclose;
htp.bodyclose;
htp.htmlclose;

END;


procedure do_something( p_old_cols in owa_util.ident_arr,
p_old_ename in owa_util.ident_arr,
p_new_ename in owa_util.ident_arr,
p_old_job in owa_util.ident_arr,
p_new_job in owa_util.ident_arr)
is

begin

for col_counter in 2 .. p_old_cols.count LOOP

-- debug ...
htp.bold('emp old# ' ||p_old_cols(col_counter)) ;
htp.br;
htp.bold('ename old ' ||p_old_ename(col_counter)) ;
htp.br;
htp.bold('enane new ' ||p_new_ename(col_counter)) ;
htp.br;
htp.bold('job old ' ||p_old_job(col_counter)) ;
htp.br;
htp.bold('job new ' ||p_new_job(col_counter)) ;
htp.br;

update emp
set ename = upper(p_new_ename(col_counter)),
job = upper(p_new_job(col_counter))
where empno = to_number(p_old_cols(col_counter)) ;

htp.bold( 'update to emp ' || p_old_cols(col_counter)||' Name '||p_new_ename(col_counter) );
if ( sql%rowcount = 0 ) then
htp.bold( ' failed, someone else changed it'||p_new_ename(col_counter) );
else
htp.bold( ' succeeded...' );
end if;
htp.paragraph ;

END LOOP;

EXCEPTION
WHEN no_data_found THEN
htp.paragraph;
htp.line;
htp.p('No data found...') ;
htp.bodyclose;
htp.htmlclose;

end;

end;
/

and Tom said...

that is the way checkboxes work. It is not an OWA issue -- it is the manner in which the web itself "works"

it is the manner in which HTTP encodes the data. The unchecked checkboxes are "not sent", there is no data for them. They quite simply "do not exist"

This is why you don't see many checkboxes on the web in general.


I prefer to use a drop down list with two elements myself. Sort of a "yes", "no" toggle. In that fashion, each "row" in the form would have a Y or N value.


Alternatively, you could have the checkboxes be "subscripts", like this:

....
L_CNT := 1;

for x in ( select '</td><td><input type="checkbox"
value="'||L_CNT||'"name=p_old_cols>'||to_char(empno) empno,
htf.escape_sc(ename) ename,
htf.escape_sc(job) job
from emp
order by empno asc ) loop

htp.p(to_char(x.empno)) ;

htp.p( '</td><td>' );
htp.formHidden( 'p_old_ename', x.ename);
htp.formText( 'p_new_ename', cvalue => x.ename );

htp.p( '</td><td>' );
htp.formHidden( 'p_old_job', x.job);
htp.formText( 'p_new_job', cvalue => x.job );

htp.p( '</td></tr>' );

end loop;


Now, in your update routine, your array from the check box would be subscripts into the other array. That is, if someone clicked on rows 1, 3, 5 and 7 -- you would get a checkbox array with the values 1, 3, 5 and 7 in it -- with which you can subscript the p_old_ename and p_new_ename array.



Actually -- truth be told -- I would hate an interface that didn't "recognize" that I changed something. Why should I have to type over someones ENAME and click a little box to tell you "please update this row". I should only have to type over the ENAME field -- that should be sufficient data for you (the programmer) to figure out what I the end user want done -- update this guys ename!




Rating

  (2 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

How to use checkbox on web form so only checked records are updated.", version 9.2.0

Leonard Anukam, February 23, 2004 - 1:46 pm UTC

Hei Tom can you shade more light on this issue or demonstrate what you mean on the later part of your answer

check boxes in arrays in PL/SQL

Raju Kakarlapudi, June 14, 2005 - 3:00 pm UTC

I am using the following function to set hidden field for check box field value.

function SetNotObservedCheckBoxValue(p_row_num) {
var v_row_num = p_row_num;
var v_not_observed_h = document.ClusterDetailsForm.not_observed_h(v_row_num-1).value;
if ( v_not_observed_h=="N" ) {
document.ClusterDetailsForm.not_observed_h(v_row_num-1).value = "Y";
} else {
document.ClusterDetailsForm.not_observed_h(v_row_num-1).value = "N";
}
}



I am calling this function with "onClick" event on check box field with passing row_number as parameter.

IF v_not_in_district = 'Y' THEN
htp.p('<input size="10%" name="not_in_district" class="OraTableCellTextCenterAlign" style="border:1px solid #f7f7e7" type="checkbox" checked nowrap value="'||v_not_in_district||'" onClick="SetNotInDisrictCheckBoxValue('||v_row_num||'); return true;" >');
htp.p('<input name="not_in_district_h" class="OraTableCellTextCenterAlign" style="border:1px solid #cccc99" nowrap type="hidden" value="'||v_not_in_district||'" >');
ELSE
htp.p('<input size="10%" name="not_in_district" class="OraTableCellTextCenterAlign" style="border:1px solid #f7f7e7" type="checkbox" nowrap value="'||v_not_in_district||'" onClick="SetNotInDisrictCheckBoxValue('||v_row_num||'); return true;" >');
htp.p('<input name="not_in_district_h" class="OraTableCellTextCenterAlign" style="border:1px solid #cccc99" nowrap type="hidden" value="'||v_not_in_district||'" >');
END IF;

I got some problem, If I don't select atleast one check box in one group. That's why I created one dummy hidden row with some dummy values.

htp.p('<input name="s_no" type="hidden" value="0000" >');
htp.p('<input name="worker_name" value="Dummy Row" type="hidden" >');
htp.p('<input name="employee_number" value="000000" type="hidden">');
htp.p('<input name="person_id" value="0000" type="hidden" >');
htp.p('<input name="program_code" value="0000" type="hidden" >');
htp.p('<input name="not_observed" value="0000" type="hidden" >');
htp.p('<input name="not_observed_h" value="0000" type="hidden" >');
htp.p('<input name="not_in_district" value="0000" type="hidden" >');
htp.p('<input name="not_in_district_h" value="0000" type="hidden" >');
htp.p('<input name="na_reason" value="0000" type="hidden" >');
htp.p('<input name="comments" value="0000" type="hidden" >');

In the target procedure I declared both check box array and dummy to check box array, but I am taking dummyto check box(hidden) field values.

PROCEDURE save_cluster_details
( s_no IN ARRAY_TYPE,
worker_name IN ARRAY_TYPE,
employee_number IN ARRAY_TYPE,
program_code IN ARRAY_TYPE,
not_observed IN ARRAY_TYPE,
not_observed_h IN ARRAY_TYPE,
not_in_district IN ARRAY_TYPE,
not_in_district_h IN ARRAY_TYPE,
na_reason IN ARRAY_TYPE,
comments IN ARRAY_TYPE ,
moment_id IN NUMBER,
person_id IN ARRAY_TYPE,
cluster_id IN VARCHAR2 DEFAULT NULL,
quarter_number VARCHAR2 DEFAULT NULL,
quarter_year VARCHAR2 DEFAULT NULL,
district_code VARCHAR2 DEFAULT NULL
);

v_s_no_array := s_no;
v_worker_name_array := worker_name;
v_employee_number_array := employee_number;
v_program_code_array := program_code;
v_not_observed_array := not_observed_h;
v_not_in_district_array := not_in_district_h;
v_na_reason_array := na_reason;
v_comments_array := comments;
v_moment_id := moment_id;
v_person_id_array := person_id;
v_cluster_id := cluster_id;
v_quarter_number := quarter_number;
v_quarter_year := quarter_year;
v_district_code := district_code;

I checked, and working fine.

Tom Kyte
June 14, 2005 - 4:25 pm UTC

sorry, i don't do javascript at all.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library