Switch to full style
Codes,problems ,discussions and solutions
Post a reply

Sending a post request using AJAX

Mon Aug 06, 2007 11:05 am

hi all ,
This application is an simple example of how you can send a POST request to an dynamic web page.Here is used php file .You can take the name of the user .Send it to the php file as a paramter and in the php file you can make the proccessing you want on it .Here i just make a echo call in the php file .In the Ajax file (html) you can take the response of your request and show it on the browser.

Here is the HTML file for the Ajax :
html code
<html >
<script type="text/javascript">
function doAjaxPost(myform)
{

var message="myname="+myform.elements["myname"].value;
var url="ajaxform.php";
var request=null;

if(window.XMLHttpRequest)
{

request=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{

request = new ActiveXObject("Microsoft.XMLHTTP");
}

if(request)
{
request.open("POST",url);
request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");

request.onreadystatechange = function() {
if(request.readyState==0)
document.getElementById('status').innerHTML='Uninitialized';
else if(request.readyState==1)
document.getElementById('status').innerHTML='Loading...';
else if(request.readyState==2)
document.getElementById('status').innerHTML='Loaded';
else if(request.readyState==3)
document.getElementById('status').innerHTML='Interactive';
else if(request.readyState==4)
{
document.getElementById('status').innerHTML='Completed';

document.getElementById("display").innerHTML =
request.responseText;
}

}
request.send(message);




}
else
document.getElementById('Txtdata').value='Nothing';
}

</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<span id='status' ></span><br/>
<form >

Name :<input type='text' size='50' name='myname'/><br/>
<input type='button' onclick="doAjaxPost(this.form);"; value="submit"/>

</form>
<div id='display'>
</div>

</body>
</html>


And here is the PHP file :
Code:

<?

  $name
=$_REQUEST['myname'];
  
   echo 
"Welcome ".$name." to AJAX world  !";
  
  ?>


i wish it will be helpful for you ,if you faced any problem while running it just reply to my post :smile:



Post a reply
  Related Posts  to : Sending a post request using AJAX
 AJAX - Sending a request to a server     -  
 check request was GET or POST     -  
 Extracting Parameters from GET or POST Request     -  
 Sending One lakh character in ajax     -  
 sending sms from bluetooth mobile to pc     -  
 Help Sending e-mail from VB.Net application     -  
 sending parameters into XSLT sheet     -  
 Sending and playing microphone audio over network     -  
 What is AJAX, How to start AJAX?     -  
 sending sms using java code and bluetoth device pls send the     -  

Topic Tags

AJAX Request