$Web Sel* example

From m204wiki
Revision as of 19:48, 20 September 2011 by JAL2 (talk | contribs) (Created page with "This page demonstrates some of the concepts of the $Web_Sel* functions. The application is a simple form which lets the user select a day of the week. To show how this can exploi...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page demonstrates some of the concepts of the $Web_Sel* functions. The application is a simple form which lets the user select a day of the week. To show how this can exploit metadata, assume that you have a Model 204 file with the days of the week in several languages, and that you have saved the user's language in a cookie named LANG. Note that by giving values of DN0, DN1, etc., the code does not need to deal with the actual day names (the option descriptions).

Assume you have records like the following:

IN META STORE RECORD TYPE = LANG LANG = ENGLISH END STORE IN META STORE RECORD TYPE = DAY LANG = ENGLISH DAY = Sunday END STORE IN META STORE RECORD TYPE = DAY LANG = ENGLISH DAY = Monday END STORE ...

$Web_Save_Sel is used to save one global select element for each language. You can do this when you start the online, but a later example shows how a language can be dynamically added. Assume the following procedure:

PROCEDURE GEN_DAYS SUBROUTINE GEN_DAYS(%LANG IS STRING LEN 20, - %NDAY IS FLOAT OUTPUT)ut) %NDAY = 0 %L1 FLOAT %L2 FLOAT %X FLOAT %L1 = $LISTNEW %L2 = $LISTNEW IN META FR WHERE TYPE=DAY AND LANG=%LANG %X = $LISTADD(%L1, 'DAY' WITH %NDAY) %X = $LISTADD(%L2, DAY) %NDAY = %NDAY + 1 END FOR IF %NDAY THEN %x = $WEB_SAVE_SEL('DAYS.' WITH %LANG, - DEL %L1, %L2) END IF END SUBROUTINE END PROC

You can use this procedure at the start of the online:

//CCAIN ... ... BEGIN IN META FR WHERE TYPE=LANG CALL GEN_DAYS(LANG, %RC) END FOR INCLUDE GEN_DAYS END

You can use the global select elements to create a drop-down list in an HTML form. $Web_Selp is used with the same second argument (the parameter name, DN) as the name= attribute of the select element; this is how you can easily "pre-select" the same day chosen before when the form is redisplayed to the user:

B %lang string len 20 %rc float %lang = $web_cookie_parm('LANG') print '<form method=post action="' with - $web_hdr_parm('URL') with'">' print 'Select day: <select name=DN>
' %RC = $web_selp('DAYS.' with %lang, 'DN') print '<input type=submit>
' print '</form>' End

This approach works fine, except that if a language is added while the online is running, a user who acquires this language in his or her cookie will have the User Language request cancelled, because $Web_Selp will not find the desired global select element. The following approach can be used to handle this situation when the web form is built. the NOCAN option of $Web_Selp is used to see if a new language is in use:

Begin %lang string len 20 %rc float %lang = $web_cookie_parm('LANG') print '<form method=post action="' with - $web_hdr_parm('URL') with '">' print 'Select day: <select name=DN>
' %rc = $web_selp('DAYS.' with %lang, 'DN', - 'NOCAN') if %rc ne 0 then call gen_days(%lang, %rc) %rc = $web_selp('DAYS.' with %lang, 'DN') end if print '<input type=submit>
' print '</form>' include gen_days End

If you want to remove a language from further processing, you can call $Web_Del_Sel:

Begin %rc = $web_del_sel('DAYS.ESPERANTO') End

However, it is then possible that a "stale cookie" may be left around in a user's browser. In that case, you will want to set a default language, but if the deleted language was Romulac, and a day beyond the 7th had been selected, you might also want to force the pre-selected day to be the middle of the week. The following code contains changes that do the following:

  • Set a default language (with $Web_Set_Cookie)
  • Use $Web_Selp_Check to ensure that the user's form parameter DN (which, remember, will contain the language-independent values DN0, DN1, etc.) is within the range of the default language. If DN is outside that range, $Web_Sel is used so that the pre-selected day is the "hand-coded" DN3.

B %lang string len 20 %rc float %lang = $web_cookie_parm('LANG') print '<form method=post action="' with - $web_hdr_parm('URL') with '">' print 'Select day: <select name=DN>
' %rc = $web_selp('DAYS.' with %lang, 'DN', - 'NOCAN') if %rc ne 0 then call gen_days(%lang, %rc) if %rc ne 0 then %lang = 'ENGLISH' %rc = $web_set_cookie('LANG', 'ENGLISH') end if %rc = $web_selp_check('DAYS.' with %lang, - 'DN') if %rc ne 0 then %rc = $web_sel('DAYS.' with %lang, 'DN3') else %rc = $web_selp('DAYS.' with %lang, 'DN') end if end if print '<input type=submit>
' print '</form>' include gen_days End