Next: , Up: (dir)

Scripting Languages G22.3033-002 Summer 2008


Previous: Top, Up: Top

Scripting Languages G22.3033-002 Summer 2008 hw07

Assigned Th 7/3/2008, due Fr 7/11 at 9pm. 50 points.

http://www.cs.nyu.edu/courses/summer08/G22.3033-002/


Next: , Up: hw07

Homework instructions

Homeworks are due on Fridays at 9pm. This deadline will be strictly enforced.

Email your answers to Robert Soulé robert.soule@gmail.com. Please put your solutions to VBA programming problems in a powerpoint presentation. For all other questions (including programming problems in other languages), just send a simple text file, such as what you get when using Emacs, Vi, Notepad, or the "save as text only" feature in Word.

Please make sure that your code works with the compilers and tools installed at CIWW. Specifically, please test:

JavaScript
Your JavaScript code must run with both Mozilla's Firefox browser and with Microsoft's Internet Explorer browser.
Php
Your PHP code must run with PHP 5.1.6 on the CIMS web servers, see http://www.cims.nyu.edu/systems/userservices/webhosting/.
Perl
Your Perl code must run with Perl 5.8.8 for Linux/x86, for example on doowop1 (see http://www.cims.nyu.edu/systems/resources/computeservers/).
Vba
Your VBA code must run with Microsoft Office 2003 for Windows on the machines in the labs CIWW 502 or CIWW 624.


Next: , Previous: Instructions, Up: hw07

Reading assignments

Read for lecture on 7/10:


Next: , Previous: hw07-readings, Up: hw07

Concept questions


Up: hw07-concepts

hw07-1 Google Suggest

(6+6 = 12 points) Try the Google Suggest web application, which is available at http://www.google.com/webhp?complete=1&hl=eN. Now think about what you would need to do to write a similar web application yourself.

  1. Briefly explain how would you use AJAX.
  2. Briefly explain where, and in what scope, you would store state.


Previous: hw07-concepts, Up: hw07

Programming exercises


Next: , Up: hw07-programming

hw07-2 Natural language translation (PHP + SQL)

(6+6+6 = 18 points) Consider the following PHP script.

     <html><head>
       <title>Translation Database</title>
     </head><body>
       <!-- show form for user input -->
       <form action="<?php echo $_SERVER[PHP_SELF]; ?>" method=get>
         <table border=0>
           <tr><td>English:    <td><input type="text" name="eng"><br/>
           <tr><td>Translated: <td><input type="text" name="tra"><br/>
         </table>
         <input type="radio" name="request" value="add">
           Add</br>
         <input type="radio" name="request" value="e2f">
           Get (English to Translated)</br>
         <input type="radio" name="request" value="f2e">
           Get (Translated to English)</br>
         <input type="radio" name="request" value="del">
           Delete</br>
         <input type="submit" value="submit">
       </form>
       <!-- open database, and create table if it does not yet exist -->
       <?php
         $db = sqlite_open("/home/hirzel/data/sqlite2", 0666, $err);
         if ($err) { die($err); }
         sqlite_query($db, "SELECT * FROM Translate", SQLITE_BOTH, $err);
         if ($err) {
           echo "Table 'Translate' does not yet exist, creating it ...<br/>";
           $q = "CREATE TABLE Translate"
              . "(English VARCHAR(50), Translated VARCHAR(50))";
           sqlite_query($db, $q, SQLITE_BOTH, $err);
           if ($err) { die($err); }
         } else {
           echo "Reopened table 'Translate'.<br/>";
         }
       ?>
       <!-- process input, if any -->
       <?php
         if ("add" == $_GET[request]) {
           $q = "INSERT INTO Translate VALUES("
              . "'" . addslashes($_GET[eng]) . "', "
              . "'" . addslashes($_GET[tra]) . "')";
           sqlite_query($db, $q);
           if ($err) { die($err); }
           echo "Add English '" . htmlentities($_GET[eng]) . "', " .
                "Translated '" . htmlentities($_GET[tra]) . "' succeeded.";
         } else if ("e2f" == $_GET['request']) {
           $q = "SELECT Translated FROM Translate WHERE English = "
              . "'" . addslashes($_GET[eng]) . "'";
           $rows = sqlite_query($db, $q);
           if ($err) { die($err); }
           $row = sqlite_fetch_array($rows, SQLITE_BOTH);
           if ($err) { die($err); }
           echo "Get English '" . htmlentities($_GET[eng]) . "' returned "
                . "Translated '" . htmlentities($row[Translated]) . "'.";
         } else if ("f2e" == $_GET['request']) {
           # put your code here
           echo "Sorry, lookup from Translated to English not yet implemented.";
         } else if ("del" == $_GET['request']) {
           # put your code here
           echo "Sorry, deletion not yet implemented.";
         }
       ?>
     </body>
  1. Set up the script to work in your own public_html directory at NYU. You will need to create your own data directory, for example, like this:
              cd ~
              mkdir data
              chmod 777 data
         

    The file permissions must enable the web server to read and write your data directory. You will also need to change the line

              $db = sqlite_open("/home/hirzel/data/sqlite2", 0666, $err);
         

    to use your own directory. When you submit this homework, please indicate the URL at which the script is running.

  2. Implement the missing reverse translation feature (lookup from Translated to English).
  3. Implement the missing deletion feature.


Next: , Previous: hw07-2, Up: hw07-programming

hw07-3 Session visit counter (PHP)

(8 points) Write a PHP script that uses a session variable to count how often the user has visited it. For example, after the second visit, the page should look like this:


Previous: hw07-3, Up: hw07-programming

hw07-4 Word reversal AJAX (JavaScript + PHP)

(6 + 6 = 12 points) Consider the AJAX application from the lecture slides from 7/3/2008, repeated here for your convenience.

a.html

     <html><head>
       <meta http-equiv="Content-Script-Type" content="text/javascript" />
       <script src="c.js"></script>
     </head><body bgColor="#00ff00">
       Time: <span id="time">(please click button)</span>
       <form name="in">
         <input type="button" value="Toggle Color" onclick="localChange();">
         <input type="button" value="Request Time" onclick="sendRequest();">
       </form>
     </body></html>

b.php

     <?php sleep(3); echo date('h:i:s') ?>

c.js

     function localChange() {
       var blue = document.bgColor == '#0000ff'
       document.bgColor = blue ? '#00ff00' : '#0000ff';
     }
     function sendRequest() {
       document.getElementById('time').innerHTML = '(please wait)';
       var xhr = false;
       function handleResponse() {
         if (4 == xhr.readyState && 200 == xhr.status)
           document.getElementById('time').innerHTML = xhr.responseText;
       }
       try { xhr = new XMLHttpRequest(); } catch(e1) {
         try { xhr = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e2) {
           try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e3) {
             alert('Could not create XMLHttpRequest object.'); } } }
       xhr.open('GET', 'b.php?nonce=' + Math.random(), true);
       xhr.onreadystatechange = handleResponse;
       xhr.send(null);
     }

Questions

  1. Install these scripts on your own CIMS web page. When you submit this homework, please indicate the URL at which the script is running.
  2. Change the application so that instead of just getting the time from the server, it reads a string from the user, sends it to the server in an AJAX call, the server reverses it, and the response handler displays it. Make sure to test your application on both Firefox and Internet Explorer. Below is a snapshot of what your application should look like.