PHP  AJAX and  xml

ajax increase the web page's interactivity, speed, and usability.

ajax can be used for communication with a xml document.


PHP ajax xml example

how a web page can fetch information from an xml document with ajax and PHP:

select a book

when the user selects a book name in the dropdown list above, at this time a background javascript code is executed. and send a request to server side php:

look this complete js code below:

    <html>
     
      <head>
        
          <script>
          
 function    showbook( book ){
            
                                    
                             
                 if  ( book <= 0 ){
                 
                    document.getelementbyid ("showtext").innerhtml="";
                    
                    return;
                    
                   }
                   
                 if ( window.xmlhttprequest ){
                 
                       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("showtext").innerhtml= xmlhttp.responsetext;
                         
                        }
                    
                    }
                    
                    
     xmlhttp.open("get", "ajaxhandler.php?q="+book, true )
                    
     xmlhttp.send();
        
               }
               
               </script>
               
             </head>
             
           <body>
           
           <div>
 
      <P>select  a user name <p>
      
       <select  name="books"  onchange = "showbook(this.value)" >
       
         <option  value="java">java</option>
         
         <option  value="php">php</option>
         
         <option  value="ajax">ajax</option>
         
         <option  value="html">html</option>
         
         <option  value="jquery">jquery</option>
         
      </select>   
  
  <div id="showtext">books datas here ... </div>
  
  </body>
  
  </html>

server side PHP File

server side php code which handle the request send by ajax and return the response:

  <?php
  
          $v =  $_get['q']; //get the value 
          
          $file =  '.\\'.$v.'.'.'xml';
  
      
             // first  load  data.xml file 
    
             $output   =  simplexml_load_file( "$file", "simplexmlelement" );

             echo     'name : '. $output->item[0]->name;   echo "<br/>";
             
             echo     'price : '. $output->item[0]->price; echo "<br/>";
             
             echo     'quantity : '. $output->item[0]->quantity;                                
    ?>