IC SunsetThe developerWorks Connections platform will be sunset on December 31, 2019. On January 1, 2020, this forum will no longer be available. More details available on our FAQ.
Topic
  • 1 reply
  • Latest Post - ‏2011-07-14T11:33:47Z by SystemAdmin
SystemAdmin
SystemAdmin
1140 Posts

Pinned topic clarification

‏2011-07-12T11:58:26Z |
Hi all,
I am new to ajax.I am using php with ajax.I need to get the elements from the database.Below in php program i have program named getuser.php in that i have names.it is like a google search.when we type one letter that entire word is displayed.
I am not understanding what is that ?q="+str after getuser.php
below is the html code. and also given the getuser.php code

<html>
<head>
<script type="text/javascript">
function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
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>

Start typing a name in the input field below:

<form>
First name: <input type="text" onkeyup="showHint(this.value)" size="20" />
</form>

Suggestions:


</body>
</html>
this is the gethint.php
<?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=$_GET;

//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a$i,0,strlen($q))))
{
if ($hint=="")
{
$hint=$a$i;
}
else
{
$hint=$hint." , ".$a$i;
}
}
}
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}

//output the response
echo $response;
?>
Updated on 2011-07-14T11:33:47Z at 2011-07-14T11:33:47Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    1140 Posts

    Re: clarification

    ‏2011-07-14T11:33:47Z  
    I am not understanding what is that ?q="+str after getuser.php
    Read, for instance, this.
    http://en.wikipedia.org/wiki/Query_string

    Apart from that, on the client-side, you've to check out js urlencodeComponent() function and the use of it in the html page. And then the non-existence of container of id txtHint.

    On the server-side, check out the proper php syntax related to lines where appearing $_GET and $a$i... They can't be working at all.