Friday, August 24, 2007

Receiving an unknown number of parameters from a web form

Here is a simple procedure that receives values from a html form, some times we don't know how many values the form will send, and we can use the owa_util.ident_arr to send and receive the values as arrays, all we need to do is use the same name for the html fields we want in the array, we can also create pairs like Oracle Portal does on its applications (p_args & p_values).



create or replace PACKAGE PARAM_TEST_PKG AS

/*
--Alternativelly you acn create the array instead of ussing owa_util
TYPE response_array IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER;
empty response_array;
*/
-- Here is an array with no values to use as defualt value for the procedure
empty owa_util.ident_arr;

PROCEDURE FORM_RESULTS (p_args in owa_util.ident_arr default empty, p_values in owa_util.ident_arr default empty) is
PROCEDURE DISPLAY_FORM;
END;
/

create or replace PACKAGE BODY PARAM_TEST_PKG AS
PROCEDURE FORM_RESULTS (p_args in owa_util.ident_arr default empty, p_values in owa_util.ident_arr default empty) is
BEGIN
--Here you process the results (i.e. insert them into a table)
htp.p('<h1>Form Results</h1>');
for i in 1..p_args.count loop
htp.p(' <B>Argument('||i||')</B>');
htp.p(' <UL><LI>Name: '||p_args(i)||'</LI><LI>Value: '||p_values(i)||'</LI></UL><BR>');
end loop;
END FORM_RESULTS;

PROCEDURE DISPLAY_FORM is
BEGIN

htp.p('<form name="add_response" method="GET" onSubmit="return validateOnSubmit()" action="/pls/portal/SCHEMA.PARAM_TEST_PKG.FORM_RESULTS">');
htp.p(' <table border="0" width="90%" cellpadding="3" cellspacing="0" >');
htp.p(' <tr>');
htp.p(' <td><b>Parameter 0</b></td>');
htp.p(' <td>');
htp.p(' <input type="hidden" name="p_args" value="Parameter 0" >');
htp.p(' <input type="text" name="p_values" size="50"> ');
htp.p(' </td>');
htp.p(' </tr>');
htp.p(' <tr>');
htp.p(' <td><b>Parameter 1</b></td>');
htp.p(' <td>');
htp.p(' <input type="hidden" name="p_args" value="Parameter 1" >');
htp.p(' <input type="text" name="p_values" size="50"> ');
htp.p(' </td>');
htp.p(' </tr>');
htp.p(' <tr>');
htp.p(' <td><b>Parameter 2</b></td>');
htp.p(' <td>');
htp.p(' <input type="hidden" name="p_args" value="Parameter 2" >');
htp.p(' <input type="text" name="p_values" size="50"> ');
htp.p(' </td>');
htp.p(' </tr>');
htp.p(' <tr>');
htp.p(' <td><b>Parameter 3</b></td>');
htp.p(' <td>');
htp.p(' <input type="hidden" name="p_args" value="Parameter 3" >');
htp.p(' <input type="text" name="p_values" size="50"> ');
htp.p(' </td>');
htp.p(' </tr>');
htp.p(' </table>');
htp.p('</form>');
END DISPLAY_FORM;

END;
/

No comments: