Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Ajax with PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 119
    Comment on it

    Ajax codes are used to communicate between web pages and the server without loading the page.

    lets demonstrate a search through ajax call with php 

    <html>
       <head>
          
          <style>
             span {
                color: green;
             }
          </style>
          
          <script>
             function showHint(str) {
                if (str.length == 0) {
                   document.getElementById("txtHint").innerHTML = "";
                   return;
                }else {
                   var xmlhttp = new XMLHttpRequest();
    					
                   xmlhttp.onreadystatechange = function() {
                      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                         document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                      }
                   }
                   xmlhttp.open("GET", "php_ajax.php?q=" + str, true);
                   xmlhttp.send();
                }
             }
          </script>
          
       </head>
       <body>
          
          <p><b>Search here:</b></p>
          
          <form>
             <input type = "text" onkeyup = "showHint(this.value)">
          </form>
          
          <p>Entered Course name: <span id="txtHint"></span></p>
       
       </body>
    </html>

    in the above code when ever there is a onkeyup event is fired in input tag text box it will call the above ajax function which will opens a file, phpajax.php with the use of GET method, 

    The file phpajax.php is as below :

    <?php
      
       $a[] = "Android";
       $a[] = "B programming language";
       $a[] = "C programming language";
       $a[] = "D programming language";
       $a[] = "euphoria";
       $a[] = "F#";
       $a[] = "GWT";
       $a[] = "HTML5";
       $a[] = "ibatis";
       $a[] = "Java";
       $a[] = "K programming language";
       $a[] = "Lisp";
       $a[] = "Microsoft technologies";
       $a[] = "Networking";
       $a[] = "Open Source";
       $a[] = "Prototype";
       $a[] = "QC";
       $a[] = "Restful web services";
       $a[] = "Scrum";
       $a[] = "Testing";
       $a[] = "UML";
       $a[] = "VB Script";
       $a[] = "Web Technologies";
       $a[] = "Xerox Technology";
       $a[] = "YQL";
       $a[] = "ZOPL";
       
       $q = $_REQUEST["q"];
       $hint = "";
       
       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";
                }
             }
          }
       }
       echo $hint === "" ? "no suggetion" : $hint;
    ?>

    the ajax call open this file and the contains the array and a parameter $q  which receives the request and work upon.

    If the requested string is matched with the array it will return the matched string in the suggestion. In this way without loading the web page the ajax call interacts with the server.   

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: