ajax call to retrieve the data from database example using jquery

index.html


<html>
     <head>
         <script type="text/javascript">
 $(document).ready(function() {
         $('.name').blur(function() {
             $.ajax({
                type: "GET",
                 dataType: "json",
                   url: "ajax_info.php",
                     data: "name="+$(this).val(),
                       success: function(json){
                         $('#number').val(json.num);
                            }
              });
         });
     });

 </script>
 </head>
     <body>
     <label>Name</label><input type="text" id="name" name="name" value="" >
     <label>Mobile Number</label><input type="text" id="number" name="number" value="" >
     </body>
 </html>

ajax_info.php

</pre>
<?php include('config.php');
$name=$_GET["name"];
$mobile_num = ' ';

$sql="SELECT customer_name FROM customer WHERE customer_name = '".$name."'";
$result = mysql_query($sql);

if(mysql_num_rows($result) > 0){

$sql="SELECT mobile_num FROM customer WHERE customer_name = '".$name."'";
$result = mysql_query($sql);
$row  = mysql_fetch_array($result);
$mobile_num = $row['mobile_num'];

}
echo'{"num":"'.$cust_num.'"}';

mysql_close($con);
?>
<pre>

If you are giving name in first text box and on blur the mobile number fills automatically, I think it may be helpful. Thanks.

This entry was posted in Ajax, Jquery, PHP and tagged , . Bookmark the permalink.
  • John

    John

  • John

    I am new Can u please provide me the complete source of this..