internet applications spring 2008. review last week –mysql –questions?

25
Internet Applications Spring 2008

Upload: martin-blankenship

Post on 11-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Internet Applications

Spring 2008

Page 2: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Review

• Last week– MySQL– Questions?

Page 3: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

This week

• Ajax• Wrap-up coverage for exercises• Class time to work on projects /

exercises

Page 4: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Ajax

• Application model diagram• Design considerations

– Graceful degradation– Application fragmentation– Usability

• Components– JavaScript– HTTP request object

Page 5: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

JavaScript • Syntax

– Function() {};– /* comment */

• Variable and function declarations– var newvariable = value;– function functionName() {content;}

• Control Structures– If (variable == value) { do stuff; }– For (var i=0; i<10; i++) { do stuff;}

• object.method.value– document.write– document.getElementById(‘navigation’);

Page 6: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

DOM JavaScript

• An approach to writing JavaScript that appends functions to elements of the HTML document

• Reference• http://www.w3schools.com/htmldom/default.asp

• getElementById• getElementsByTagName• parentNode• childNodes• nodeValue• firstChild• previousSibling

Page 7: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Ajax Basics

• Combination of JavaScript, browser functions, and data– Asynchronous JavaScript and XML

• Reproduces the GET/POST functionality of our forms

• Works with any data stream• Can only request data from originating

server

Page 8: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Ajax GET/POST

• GET– Retrieves a document from the server and

allows client side document processing• POST

– Sends a querystring to server and enables complex data retrieval

Page 9: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

responseText, responseXML

• Two forms of from xmlHTTPRequest • responseText

– Returns any text (HTML, JSON, etc) which can be manipulated by JavaScript after processing

• responseXML– Returns XML data that can be manipulated

directly

Page 10: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Ajax Exercise

• Create a simple HTML page that contains no real content.

• Use a link to retrieve a document from the server and display it in our browser

• Skills– Utilizes Ajax GET method– Utilizes some DOM elements

• document.getElementById("bodymain");• details.innerHTML = request.responseText;

Page 11: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Ajax Function Framework

getSomething()

Initiate Connection

Page 12: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

getHttpObject()

• Responsible for creating XMLHttpRequest

function getHTTPObject() {

var xhr = false;

if (window.XMLHttpRequest) {

xhr = new XMLHttpRequest();

} else if (window.ActiveXObject) {

xhr = new ActiveXObject("Microsoft.XMLHTTP");

}

return xhr;

}

Page 13: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

getSomething()

• Responsible for initiating a connection

function getSomething(file) {

var request = getHTTPObject();

if (request) {

request.onreadystatechange = function() {

displayResponse(request);

};

request.open("GET", file, true)

request.send(null);

}

}

Page 14: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

getSomething()

Responsible for monitoring connectionfunction displayResponse(request) {

if (request.readyState == 4) {

if(request.status == 200 || request.status == 304) {

var details = document.getElementById("bodymain");

details.innerHTML = request.responseText;

} else {

alert(request.status);

}

}

}

Page 15: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

A quick POST example

function updateFeed(file, query) {

var request = getHTTPObject();

if (request) {

request.onreadystatechange = function() {

displayResponse(request);

};

request.open ("POST", file, true);

request.setRequestHeader ("Content-Type",

"application/x-www-form-urlencoded");

request.send(query);

}

}

Page 16: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Element manipulation with DOM

• //Delete ElementselementToDelete = document.getElementById("bodymain");

parentElement = elementToDelete.parentNode;

parentElement.removeChild(elementToDelete);

• //Create a new body elementbodyElement = document.createElement("body");

bodyElement.setAttribute("id", "bodymain");

parentElement.appendChild(bodyElement);

• //Create a Text NodenewLink = document.createElement("a");

newLink.setAttribute("href", "#");

newLink.setAttribute("onclick", "getSomething('./htmlfragment.html')");

linkText = document.createTextNode("Get some content");

newLink.appendChild(linkText);

newBody.appendChild(newLink);

Page 17: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Putting it all together

• Part I– Create a basic HTML page– Download the HTML fragment– Create your JavaScript functions– Insert a link to initiate the Ajax Request

• Part II– Create a new JavaScript function that will

use the DOM– Modify the Ajax Request to include a ‘delete

content’ link

Page 18: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Skills needed for exercises 8 & 9

• Ex 8 – External PHP functions file• PHP require() function• File management

• Ex 9 – Javascript• Basics of JavaScript functions• DOM scripting

Page 19: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Skills needed for exercises 10 & 11

• Ex 10 – Login / Logoff functions• More on HTML forms & PHP• Writing and using Cookies

• Ex 11 - Ajax• Ajax components• Advanced JavaScript functions

Page 20: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

Next week

• Final Projects due• Quick presentation of class projects (1-2

min per person)• Class wrap-up & evaluation

Page 21: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

MySQL

• Open Source Relational Database• http://mysql.com• At SILS

– pearl.ils.unc.edu• Relational database features

• Tables, Indexes, Queries• SQL (Structured Query Language)

Page 22: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

SQL Skills

• SQL – Structured query language– Uses a syntax with words like (select,

where, insert, delete, from) to create logical statements• Select column from tablename where limit =

‘value’;• Insert into table (column, column) values (value

1, value 2);

– A good reference• http://www.w3schools.com/sql/sql_quickref.asp

Page 23: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

SQL Examples

• SELECT statements• SELECT * from feeds where username = 'mitcheet'", • SELECT * from feeds where id = ".$_REQUEST['feed']

• INSERT statements• INSERT INTO feeds (username, feedname, feedURL)

values ('".$_REQUEST['username']."', '".$_REQUEST['feedName']."', '".$_REQUEST['feedUrl']."')";

• DELETE statements• DELETE from feeds where id = ".$_REQUEST['feedId']

Page 24: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

MySQL functions in PHP• Create a connection to the database

• $connection = mysql_connect($dbserver, $username, $pass);

• Select a database• mysql_select_db($database, $connection);

• Run a query• $result = mysql_query("SELECT * from feeds where username =

'mitcheet'", $connection);

• Get the results of the query as an array• while ($row = mysql_fetch_array($result, MYSQL_NUM)) {}

• Close the connection• mysql_close($connection);

Page 25: Internet Applications Spring 2008. Review Last week –MySQL –Questions?

MySQL Examplefunction showFeed () {

$connection = mysql_connect ("pearl.ils.unc.edu", "inls572_spring08",

"yreuq572");

mysql_select_db("inls572_spring08", $connection);

$result = mysql_query("SELECT * from feeds where id = ".$_REQUEST['feed'], $connection);

while ($row = mysql_fetch_array($result, MYSQL_NUM)){

echo $row[3];}

}