external next up previous contents index
Next: Dealing with cgi via Up: 8.8 [TECFAMOO] ``E_WEB'' Tutorial Previous: 8.8 [TECFAMOO] ``E_WEB'' Tutorial

Dealing with cgi queries via the /cgi URL

 

At TECFAMOO the are 2 ways to deal with cgi queries. Either you deal within the :http_request verb with a query or you use un URL like:

             http://tecfa.unige.ch:7778/cgi/object
We shall first consider this latter case which will not work on other E_Web MOOs than TECFAMOO.

Here are two examples of of a typical ``/cgi'' URL.

<FORM METHOD="POST" ACTION="http://tecfa.unige.ch:7778/cgi/&e_thing">
                       or
<FORM METHOD="POST" ACTION="http://tecfa.unige.ch:7778/cgi/4026">

In order to deal with a query you need to produce one. Here you will learn how to do it with an html form and the POST method. You don't need to produce this form with E_Web, you can deal with cgi queries from any web page (sitting on an ordinary web server), as shown here for example. Ususally if you use ``canned'' forms, i.e. forms that don't change its contents, it is faster to serve them from your standard www server.

Now let's look at a simple example. It queries two strings from a user and then gives some feedback. You can have a look at it before you move on.

When you create the form with E_Web, you have the choice between creating the form dynamically or using a canned form. In our example, the form is simply inserted in the .htext property whose contents will be inserted automatically into the web page by objects like the Generic E_WEBbed thing as explained in section 8.6 above. Note that if you want to change the looks of the generated page you will have to modify the http_request verb (e.g. by copying it from the parent).

So, we first create an object:

         @create &e_thing named Hello CGI example, helloex

Next, we add a form into the .htext property:

@set-note-text #4848.htext
<FORM METHOD="POST" ACTION="http://tecfa.unige.ch:7778/cgi/4848">
<CENTER><H1>Simple Hello Example</H1></CENTER>
<br>
What is your name:
<input type="text" size=20 name=name><br>

Your age:
<input type="radio" name=age value="young" checked> &lt; 30
<input type="radio" name=age value="old"> &gt; 30
<p>
Input something please:<br>
<input type="text" name=string size=60><br>

<HR width=250><BR>
<input type="submit" value="Alors / Let's see !?!">
</FORM>
.
Note that we post the forms content to the same object (which is #4848 here).

Now to compute the form input, all you need to program is a verb called cgi_query on the object addressed by the /cgi/ URL. (Note that the object serving the form and computing the query is the same here, but this does not need to be the case).

In order to generate interesting html it is a good idea to learn how to use the verbs in the $ehtml_utils. All the rest is taken care off by the server.

Here is the code of the cgi_query verb:

@program #4848:cgi_query   this none this
 1:  "This script will process a query from http://tecfa.unige.ch:7778/4848";
 2:  "e.g. return some simple feedback";
 3:  //
 4:  query_string = args[1];
 5:  //
 6:  //            BUILD TITLE OF THE PAGE
 7:  //
 8:  title = "Query results";
 9:  result = {$ehtml_utils:title(title), $ehtml_utils:h1(title)};
10:  //
11:  //            COMPUTE FORM (query string) and build USER FEEDBACK
12:  //
13:  age = $ehtml_utils:form_urldecode_value("age", query_string);
14:  name = $ehtml_utils:form_urldecode_value("name", query_string);
15:  string = $ehtml_utils:form_urldecode_value("string", query_string);
16:  result = {@result, "Bonjour/Hello ", age, " ", name, ". ", $ehtml_utils:p()};
17:  result = {@result, "You said: ", $ehtml_utils:b(string), "<p><hr>"};
18:  result = {@result, "Just for your info: the query string was: ", query_string};
19:  //
20:  //            RETURN RESULTS
21:  //
22:  return {@result};

Let's first see how we decode the query string: The query_string is the first argument handed to the verb. In order to make the code more understandable we put it into the query_string variable on line 3. In the $e_html_utils we have a form_url_decode_value verb that will give us the value for a given html tag name. Look at lines 13 to 15.

Once we have stored away the information we want in some variables, we can compute them. In our case we simply print them back. I.e. in lines 16 and 17 we add their contents to the result that will be returned by this function. Just FYI we also output the raw query string in line 18.

Not let's look at a more complex example of a ``canned'' form (in french) served by E_Web. Try it out first!! It will compute the maintenance cost of a car according to several critera, and also store the information (each query) on some object. Look at the code and try to understand it (yes I know, explanations are under construction):

@program #4198:cgi_query   this none this
 1:  "This script will get a query from http://tecfa.unige.ch:7778/cgi/4198";
 2:  "do some computation on the data, keep the data, and report results";
 3:  "NOTE that you should make verbs and writable properties unreadable";
 4:  "if you wish to have some security.";
 5:  hu = $ehtml_utils;
 6:  query_string = args[1];
 7:  flag = hu:form_urldecode_value("summary", query_string) == "on" ? 1 | 0;
 8:  //
 9:  //            BUILD TITLE OF THE PAGE
10:  //
11:  title = "R&eacute;sultats du calcul";
12:  result = {hu:title(title), hu:h1(title)};
13:  if (!flag)
14:    //
15:    //            COMPUTE FORM (query string) and build USER FEEDBACK
16:    //            (only if summary == "off")
17:    //
18:    nom = hu:form_urldecode_value("nom", query_string);
19:    result = {@result, "Bonjour/Hello ", nom, ". ", hu:p()};
20:    assurance = hu:form_urldecode_num_value("assurance", query_string);
21:    accidents = hu:form_urldecode_num_value("accidents", query_string);
22:    consokilo = hu:form_urldecode_num_value("consokilo", query_string);
23:    prixcarbu = hu:form_urldecode_num_value("prixcarbu", query_string);
24:    kilomois = hu:form_urldecode_num_value("kilomois", query_string);
25:    vignette = hu:form_urldecode_num_value("vignette", query_string);
26:    tcs = hu:form_urldecode_num_value("tcs", query_string);
27:    autoroutes = hu:form_urldecode_num_value("autoroutes", query_string);
28:    entretien = hu:form_urldecode_num_value("entretien", query_string);
29:    amendes = hu:form_urldecode_num_value("amendes", query_string);
30:    // Attention aux floating point numbers ici !
31:    cost = floor((accidents + assurance + consokilo / 100.0 * kilomois * prixcarbu * 12.0 + vignette + tcs + autoroutes + entretien + amendes) / 12.0);
32:    result = {@result, "Votre bagnole vous coutera environ ", cost, " francs par mois / Your car costs about ", cost, " Swiss francs/month.<br>"};
33:    if (cost < 400.0)
34:      evaluation = "Vous en sortez bien / This is fine";
35:    else
36:      evaluation = "Vous ne vous en sortez pas bien, pensez aux transports communs / You pay quite a lot!";
37:    endif
38:    result = {@result, hu:b(evaluation)};
39:    //
40:    //                      REGISTER DATA
41:    //
42:    this.data_size = this.data_size + 1;
43:    date = $time_utils:ddmmyy(time());
44:    time = $time_utils:hms(time());
45:    this.all_data = {@this.all_data, {this.data_size, nom, assurance, accidents, consokilo, prixcarbu, kilomois, vignette, tcs, autoroutes, entretien, amendes, cost, date, time}};
46:  endif
47:  //
48:  //                       COMPUTE GLOBAL RESULTS
49:  //
50:  totresult = {hu:h2("Ensemble des requetes")};
51:  table = hu:table_header({"no", "nom", "assu-<br>rance", "accidents", "conskilo", "prix<br>carbu", "kilomois", "vignette", "tcs", "auto-<br>routes", "entretien", "amendes", "cost", "date", "time"});
52:  means = $list_utils:make(11, 0.0);
53:  for line in (this.all_data)
54:    table = {@table, hu:table_row(line)};
55:    " ---- do some statistics at the same time";
56:    for element in [1..length(line) - 4]
57:      means[element] = means[element] + line[element + 2];
58:    endfor
59:  endfor
60:  for el in [1..length(means)]
61:    means[el] = floatstr(means[el] / tofloat(this.data_size), 1);
62:  endfor
63:  table = {@table, hu:table_row({"", "Moyenne", @means})};
64:  table = hu:table(table, 1);
65:  totresult = {@totresult, @table};
66:  //
67:  //                       INFORMATION ON CGI QUERY STRING
68:  //
69:  info = hu:form_urldecode_all(query_string);
70:  debug = {};
71:  debug = {@debug, hu:hr(3, 500), hu:h2("Pour information")};
72:  debug = {@debug, "Your query to ", this.name, " <", &object_browser:object_anchor(this), "> ", "contained the following:<P>"};
73:  debug = {@debug, "Query string was =", query_string, "<hr>'$ehtml_utils:form_urldecode_all' returned: <br>{"};
74:  for x in (info)
75:    debug = {@debug, tostr("{", x[1], length(x) > 1 ? tostr(" , ", x[2]) | "", "},")};
76:  endfor
77:  debug = {@debug, "}", "<hr>", "See: ", &object_browser:object_anchor($ehtml_utils), "<hr>"};
78:  //debug = {@debug, $cgi:("html_test-cgi")()};
79:  //
80:  //                      RETURN THE RESULTS
81:  //
82:  return {@result, @totresult, @debug};
83:  /* Last edited on Wed Jan 15 17:05:44 1997 GMT+1 by Kaspar (#85). */


next up previous contents index external
Next: Dealing with cgi via Up: 8.8 [TECFAMOO] ``E_WEB'' Tutorial Previous: 8.8 [TECFAMOO] ``E_WEB'' Tutorial

Daniel K. Schneider
Thu Apr 17 12:43:52 MET DST 1997