Next: , Up: (dir)

Scripting Languages G22.3033-002 Summer 2008


Previous: Top, Up: Top

Scripting Languages G22.3033-002 Summer 2008 Example solutions for 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: solutions-hw07

Instructions for example solutions

These are example solutions. Please keep in mind that often, there is not just one correct solution to a question. If you come up with different answers, then it may be that both your answers and these answers here are correct. Of course, these answers here may also contain mistakes. If you spot a mistake, please let me know so I can correct it.


Previous: Instructions, Up: solutions-hw07

Example solutions for Homework 7


Next: , Up: Solutions-hw07

solutions-hw07-1 Google Suggest

  1. Each time the user changes a character in the search box, I would send an XMLHttpRequest to the server with the entire contents of the search box. Then, the server would send back a list of suggested completions.
  2. The server would store a database of suggestions at application scope. All users would use the same database, it would survive longer than any individual request or even session.


Next: , Previous: solutions-hw07-1, Up: Solutions-hw07

solutions-hw07-2 Natural language translation (PHP + SQL)

  1. http://www.cs.nyu.edu/~hirzel/hw07/2.php
  2.           <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']) {
                    # echo "Sorry, lookup from Translated to English not yet implemented.";
                    $q = "SELECT English FROM Translate WHERE Translated = "
                       . "'" . addslashes($_GET[tra]) . "'";
                    $rows = sqlite_query($db, $q);
                    if ($err) { die($err); }
                    $row = sqlite_fetch_array($rows, SQLITE_BOTH);
                    if ($err) { die($err); }
                    echo "Get Translated '" . htmlentities($_GET[tra]) . "' returned "
                         . "English '" . htmlentities($row[English]) . "'.";
                  } else if ("del" == $_GET['request']) {
                    # echo "Sorry, deletion not yet implemented.";
                    $q = "DELETE FROM Translate WHERE "
                       . "English = '" . addslashes($_GET[eng]) . "' AND "
                       . "Translated = '" . addslashes($_GET[tra]) . "'";
                    sqlite_query($db, $q);
                    if ($err) { die($err); }
                    echo "Delete English '" . htmlentities($_GET[eng]) . "', " .
                         "Translated '" . htmlentities($_GET[tra]) . "' succeeded.";
                  }
                ?>
              </body>
         


Next: , Previous: solutions-hw07-2, Up: Solutions-hw07

solutions-hw07-3 Session visit counter (PHP)

     <?php session_start(); ?>
     <html><head>
       <title>Session counter</title>
     </head><body>
       In this session, you have visited this page
       <?php
         if (empty($_SESSION[count]))
           $_SESSION[count] = 0;
         $_SESSION[count]++;
         echo $_SESSION[count];
       ?>
       times.
     </body>


Previous: solutions-hw07-3, Up: Solutions-hw07

solutions-hw07-4 Word reversal AJAX (JavaScript + PHP)

  1. http://www.cs.nyu.edu/~hirzel/a.html
  2. a.html
              <html><head>
                <meta http-equiv="Content-Script-Type" content="text/javascript" />
                <script src="c.js"></script>
              </head><body bgColor="#00ff00">
                Reversed: <span id="reverse">(please click button)</span>
                <form name="in">
                  String to reverse: <input type="text" name="forward"><br/>
                  <input type="button" value="Toggle Color" onclick="localChange();">
                  <input type="button" value="Reverse String" onclick="sendRequest();">
                </form>
              </body></html>
         

    b.php

              <?php sleep(3); echo htmlentities(strrev($_GET[fwd])) ?>
         

    c.js

              function localChange() {
                var blue = document.bgColor == '#0000ff'
                document.bgColor = blue ? '#00ff00' : '#0000ff';
              }
              function sendRequest() {
                document.getElementById('reverse').innerHTML = '(please wait)';
                var xhr = false;
                function handleResponse() {
                  if (4 == xhr.readyState && 200 == xhr.status)
                    document.getElementById('reverse').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?fwd=' + document.in.forward.value, true);
                xhr.onreadystatechange = handleResponse;
                xhr.send(null);
              }