PHP  AJAX and mysql database

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

ajax can be used for communication with a database.


PHP Ajax MySql Example

how a web page can fetch datas from a database server with php and ajax:

select a user name

when the user selects a user 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    ajaxdata(value){
            
                                    
                             
                 if  ( value <= 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="+value, true )
                    
     xmlhttp.send();
        
               }
               
               </script>
               
             </head>
             
           <body>
           
           <div>
 
      <P>select  a user name <p>
      
       <select  name="users"  onchange = "ajaxdata(this.value)" >
       
         <option  value="sandeep">sandeep</option>
         
         <option  value="ankit">ankit</option>
         
         <option  value="vinod">vinod</option>
         
         <option  value="vivek">vivek</option>
         
         <option  value="bill gates">bill gates</option>
         
      </select>   
  
  <div id="showtext">show complete data first select user </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'];
          
          // create a database connection
          
          $sql_con  = mysqli_connect('localhost', 'sandeep', 'ilike', 'users'); 
          
          if( !sql_con){
          
             die( mysqli_error($sql_con) );
             
           }
           
           $data  = "select *  from  user  where id = ".$v.";
           
           $output  = mysqli_query( $sql_con, $data);  
          
           mysqli_close($sql_con);
               
     ?>