Collection Contents Index Improving the template Working with forms pdf/chap10.pdf

First Guide to SQL Anywhere Studio
   PART 2. Getting Results with SQL Anywhere Studio
     CHAPTER 10. Building a Dynamic Web Site with PowerDynamo       

Working with DynaScript


In addition to embedded queries, formatting, and static HTML, you can add application logic to templates with the DynaScript scripting language. You can also equip templates to process parameters passed from a Web browser appended to the URL or submitted via an HTML form.

This project involves the following tasks:

Top of page  About DynaScript

You can embed scripts in HTML templates to control the information that the template provides to the user. The Application server interprets DynaScript scripts and the results of that interpretation are passed on to the Web server which are then passed on to the browser.

The DynaScript scripting language uses a syntax much like JavaScript. It has the following characteristics:

For more information on the DynaScript and DynaScript objects, see the PowerDynamo User's Guide.

Top of page  Add DynaScript to the template

Now that our template is querying the database we want to check that the query was completed without error. We can check for errors during processing by adding some DynaScript to our template.

  1. Open the Employees.stm template in Sybase Central.

  2. Place the cursor immediately following the </TABLE> tag.

  3. Insert a carriage return, and type these two lines of code, each followed by a carriage return:

    <!--SCRIPT
    -->

    These tags delimit DynaScript.

  4. Type the following lines of code within the script delimiters, each followed by a carriage return:

    document.WriteLn("The query");
        if (SQL.GetErrorCode()==0) {
                document.WriteLn( " succeeded." );
        } else {
            document.WriteLn( " failed." );
        }

    The above code checks for an error code and if 0 is returned outputs "The query succeeded" to the browser. If an error code of anything other than 0 is returned, the output to the browser will be "the query failed".

  5. Choose File->Save to Database, and reload or refresh the document in the Web browser.

Top of page  About script error messages

If you receive an error warning, return to the open template and fix the error. Save the changes to the database, as above, and reload or refresh the document in the Web browser.

Top of page  Pass an argument to a template

Users can supply an argument when they request a document, which will be customized for the value they supply. The usual way to supply an argument is to fill in a form (you will work with forms in the next task).

The simplest way to supply an argument is to add it at the end of the URL, as in the following string:

?argumentname=value

In this task, you create an argument called name, reference it from within HTML and DynaScript, and supply the value appended to the URL that you request.

  1. Return to the open Employee.stm template.

  2. Place the cursor above the script element that you added in the previous step. The cursor should be immediately after the </TABLE> tag.

  3. Add a carriage return, and type the following:

Hello, $name!<P>

You can refer to an argument directly in the HTML and query portions of a template, where its name is preceded with the $ (dollar sign). This is called a text replacement macro. PowerDynamo substitutes the text replacement macro with the value passed in as an argument before sending the query.

Choose File->Save To Database.

  1. Request the following URL in the Web browser, substituting your name for yourname:

    http://localhost/ASASite/Employees.stm?name=yourname

    The document should display your name.

  2. Close the Employees.stm template.

Top of page  

Collection Contents Index Improving the template Working with forms pdf/chap10.pdf