Thursday, 19 March 2015

Ajax _example _code_ for _selecting _ data _from _database _Using_php


(1) write code for  "Ajax.html" file.


<html>
<head>
<script  type="text/JavaScript">


function PostData() {
    // 1. Create XHR instance - Start
    var xhr;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }
    else {
        throw new Error("Ajax is not supported by this browser");
    }
    // 1. Create XHR instance - End
   
    // 2. Define what to do when XHR feed you the response from the server - Start
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status == 200 && xhr.status < 300) {
                document.getElementById('div1').innerHTML = xhr.responseText;
            }
        }
    }
    // 2. Define what to do when XHR feed you the response from the server - Start

    var userid = document.getElementById("userid").value;

    // 3. Specify your action, location and Send to the server - Start
    xhr.open('POST', 'verify.php');
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("userid=" + userid);
    // 3. Specify your action, location and Send to the server - End
}


</script>

</head>
<body>
<form>
    <label for="userid">User ID :</label>
    <input type="text" name ="userid" id="userid" onblur="PostData()" />
    <div id="div1"></div>
    <input type="button" value ="Check" onclick="PostData()" />
</form>

</body>
</html>



(2)write code for "verify.php" :

<?php

  $userid = trim($_POST["userid"]);
 
 
  $con=mysql_connect("localhost","root","");
 
 
  mysql_select_db("itexam",$con);
 
 
  $sql="select email  from studentrecord  where email='$userid'";
 
 
  $result=mysql_query($sql);
 
 
  $num=mysql_num_rows($result);
 
 
  if($num>0)
 
  {
 
  echo "email id used alreday ";
 
 
  }
 
  else
 
 
 
  {
 
  echo "email id is available";
 
 
  }
 
 


?>

No comments:

Post a Comment