ajax code examples 1 - csuohio.educis.csuohio.edu/~sschung/cis408/ajax code examples_1.pdfwhen the...

21
AJAX Code Examples When form element is created with two input boxes and a Submit button: <!DOCTYPE html> <html> <body> <form action="/action_page.php"> First name:<br> <input type="text" name="firstname" value="Mickey"> <br> Last name:<br> <input type="text" name="lastname" value="Mouse"> <br><br> <input type="submit"> </form> </body> </html>

Upload: others

Post on 25-Apr-2020

23 views

Category:

Documents


0 download

TRANSCRIPT

AJAX Code Examples

When form element is created with two input boxes and a Submit button:

<!DOCTYPE html>

<html>

<body>

<form action="/action_page.php">

First name:<br>

<input type="text" name="firstname" value="Mickey">

<br>

Last name:<br>

<input type="text" name="lastname" value="Mouse">

<br><br>

<input type="submit">

</form>

</body>

</html>

When the button is clicked, web browser create XMLHTTPRequest to the server side script

action_page.php with the parameters contructed from user input in two input boxes as written below in

Server response text.

<!DOCTYPE html>

<html>

<body>

<h1>The XMLHttpRequest Object</h1>

<h2>Retrieve data from XML file</h2>

<p><b>Status:</b> <span id="A1"></span></p>

<p><b>Status text:</b> <span id="A2"></span></p>

<p><b>Response:</b> <span id="A3"></span></p>

<button onclick="loadDoc('note.xml')">Get XML data</button>

<script>

function loadDoc(url) {

var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {

if (this.readyState == 4 && this.status == 200) {

document.getElementById('A1').innerHTML = this.status;

document.getElementById('A2').innerHTML = this.statusText;

document.getElementById('A3').innerHTML = this.responseText;

}

};

xhttp.open("GET", url, true);

xhttp.send();

}

</script>

</body>

</html>

After Clicking the Button:

<!DOCTYPE html>

<html>

<body>

<h1>The XMLHttpRequest Object</h1>

<h3>Start typing a name in the input field below:</h3>

<form action="">

First name: <input type="text" id="txt1" onkeyup="showHint(this.value)">

</form>

<p>Suggestions: <span id="txtHint"></span></p>

<script>

function showHint(str) {

var xhttp;

if (str.length == 0) {

document.getElementById("txtHint").innerHTML = "";

return;

}

xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {

if (this.readyState == 4 && this.status == 200) {

document.getElementById("txtHint").innerHTML = this.responseText;

}

};

xhttp.open("GET", "gethint.php?q="+str, true);

xhttp.send();

}

</script>

</body>

</html>

The PHP File - "gethint.php"

The PHP file checks an array of names, and returns the corresponding name(s) to the browser:

<?php

// Array with names

$a[] = "Anna";

$a[] = "Brittany";

$a[] = "Cinderella";

$a[] = "Diana";

$a[] = "Eva";

$a[] = "Fiona";

$a[] = "Gunda";

$a[] = "Hege";

$a[] = "Inga";

$a[] = "Johanna";

$a[] = "Kitty";

$a[] = "Linda";

$a[] = "Nina";

$a[] = "Ophelia";

$a[] = "Petunia";

$a[] = "Amanda";

$a[] = "Raquel";

$a[] = "Cindy";

$a[] = "Doris";

$a[] = "Eve";

$a[] = "Evita";

$a[] = "Sunniva";

$a[] = "Tove";

$a[] = "Unni";

$a[] = "Violet";

$a[] = "Liza";

$a[] = "Elizabeth";

$a[] = "Ellen";

$a[] = "Wenche";

$a[] = "Vicky";

// get the q parameter from URL

$q = $_REQUEST["q"];

$hint = "";

// lookup all hints from array if $q is different from ""

if ($q !== "") {

$q = strtolower($q);

$len=strlen($q);

foreach($a as $name) {

if (stristr($q, substr($name, 0, $len))) {

if ($hint === "") {

$hint = $name;

} else {

$hint .= ", $name";

}

}

}

}

// Output "no suggestion" if no hint was found or output correct values

echo $hint === "" ? "no suggestion" : $hint;

?>

<!DOCTYPE html>

<html>

<body>

<div id='showCD'></div><br>

<input type="button" onclick="previous()" value="<<">

<input type="button" onclick="next()" value=">>">

<script>

var i = 0;

displayCD(i);

function displayCD(i) {

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {

if (this.readyState == 4 && this.status == 200) {

myFunction(this, i);

}

};

xmlhttp.open("GET", "cd_catalog.xml", true);

xmlhttp.send();

}

function myFunction(xml, i) {

var xmlDoc = xml.responseXML;

x = xmlDoc.getElementsByTagName("CD");

document.getElementById("showCD").innerHTML =

"Artist: " +

x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +

"<br>Title: " +

x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +

"<br>Year: " +

x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue;

}

function next() {

if (i < x.length-1) {

i++;

displayCD(i);

}

}

function previous() {

if (i > 0) {

i--;

displayCD(i);

}

}

</script>

</body>

</html>

<!DOCTYPE html>

<html>

<style>

table,th,td {

border : 1px solid black;

border-collapse: collapse;

}

th,td {

padding: 5px;

}

</style>

<body>

<p>Click on a CD to display album information.</p>

<p id='showCD'></p>

<table id="demo"></table>

<script>

var x,xmlhttp,xmlDoc

xmlhttp = new XMLHttpRequest();

xmlhttp.open("GET", "cd_catalog.xml", false);

xmlhttp.send();

xmlDoc = xmlhttp.responseXML;

x = xmlDoc.getElementsByTagName("CD");

table="<tr><th>Artist</th><th>Title</th></tr>";

for (i = 0; i <x.length; i++) {

table += "<tr onclick='displayCD(" + i + ")'><td>";

table += x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue;

table += "</td><td>";

table += x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue;

table += "</td></tr>";

}

document.getElementById("demo").innerHTML = table;

function displayCD(i) {

document.getElementById("showCD").innerHTML =

"Artist: " +

x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +

"<br>Title: " +

x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +

"<br>Year: " +

x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue;

}

</script>

</body>

</html>

After Clicking on the first table row:

<!DOCTYPE html>

<html>

<style>

table,th,td {

border : 1px solid black;

border-collapse: collapse;

}

th,td {

padding: 5px;

}

</style>

<body>

<h1>The XMLHttpRequest Object</h1>

<form action="">

<select name="customers" onchange="showCustomer(this.value)">

<option value="">Select a customer:</option>

<option value="ALFKI">Alfreds Futterkiste</option>

<option value="NORTS ">North/South</option>

<option value="WOLZA">Wolski Zajazd</option>

</select>

</form>

<br>

<div id="txtHint">Customer info will be listed here...</div>

<script>

function showCustomer(str) {

var xhttp;

if (str == "") {

document.getElementById("txtHint").innerHTML = "";

return;

}

xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {

if (this.readyState == 4 && this.status == 200) {

document.getElementById("txtHint").innerHTML = this.responseText;

}

};

xhttp.open("GET", "getcustomer.asp?q="+str, true);

xhttp.send();

}

</script>

</body>

</html>

After drop box selection: