Home  • University • Campus

How to call AJAX function by native JavaScript user defined function

<html>
<head>
 <title>AJAX Example</title>
<script>
function ajaxGET(div,url){
	var xmlhttp;
	if (window.XMLHttpRequest){		
	  xmlhttp=new XMLHttpRequest();
	}else{
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}	
	xmlhttp.onreadystatechange=function(){
	  if(xmlhttp.readyState==4 && xmlhttp.status==200){
		document.getElementById(div).innerHTML=xmlhttp.responseText;
	  }
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}
</script>
</head>
<body>
<form name='frmStu'>
<input type='text' name='txtId' />
<input type='button' value='Show' onclick="ajaxGET('output','student.php?id='+document.frmStu.txtId.value)" />
</form>
<div id='output'></div>
</body>
</html>
student.php
 if(isset($_GET["id"])){
   $id=$_GET["id"];
   $db=new mysqli("localhost","root","","test");
   $table=$db->query("select id,name,phone from student where id='$id'");
   list($id,$name,$phone)=$table->fetch_row();
   echo $id," ",$name," ",$phone;

}

Comments 0


Copyright © 2025. Powered by Intellect Software Ltd