This following code uses javascript for Ajax, it needs to be modified to use Doj
ID: 3568626 • Letter: T
Question
This following code uses javascript for Ajax, it needs to be modified to use Dojo for Ajax.
Create 2 files: showhint.html, and gethint.php. These two files are given
<html>
<head>
<script>
function showHint(str) {
if (str.length==0) {
document.getElementById("txtHint").innerHTML="";
return;
}
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gethint.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<p><b>Start typing a name in the input field below:</b></p>
<form>
First name: <input type="text">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
<?php
// Fill up 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 the correct values
echo $hint==="" ? "no suggestion" : $hint;
?>
Explanation / Answer
You need to modify the "showhint.html" file and leave the "gethint.php" exactly as it is.
NEW SHOWHINT.HTML:-
<html>
<head>
<script>
function showHint(str) {
if (str.length==0) {
dojo.byId("txtHint").innerHTML="";
return;
}
dojo.xhrGet({
// The URL to request
url: "gethint.php",
data: {
q:str
},
// The method that handles the request's successful result
// Handle the response any way you'd like!
load: function(result) {
dojo.byId("txtHint").innerHTML=result;
}
});
}
</script>
</head>
<body>
<p><b>Start typing a name in the input field below:</b></p>
<form>
First name: <input type="text">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.