1st time for AJAX, I do not understand any line of codes

I am new to programming and my first time learning AJAX with PHP. I got a working code example from the Internet and studied it, but there are some codes that I don’t understand, and I'm really upset.

As index.phpI was so lost in your code xmlhttp.open("GET","gethint.php?q="+str,true);. I do not know what it means q. As far as I understand, qshould stand for an html element with a name q. For example, I have <input type="text" name="q" />, then I know that I have a text name q. But in this example, I cannot find any element with a name q. Help Pls ...

index.php

<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>

<p><b>Start typing a name in the input field below:</b></p>
<form> 
 First name: <input type="text" onkeyup="showHint(this.value)" size="20" />
</form>
<p>Suggestions: <span id="txtHint"></span></p>

</body>
</html>


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["q"];

 //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;
?>
+3
source share
3 answers

q - , gethint.php , str.

"", , ( onkeyup).

q PHP $q=$_GET["q"];.

, PHP, , HTML.

+4

its parameter is LIKE A URL, which is close to

PHP

gethint.php?q=mYstring

echo $_GET['q'];// output as mYstring

In js,

var str ='mYstring';
"gethint.php?q="+str

echo $_GET['q'];// output as mYstring

i thing you can get if any problem adds comments ..pal (Required)

+1
source

All Articles